Structuring Javascript Testing in .NET / Visual Studio

I have a .NET MVC solution, it contains various javascripts, I want to test these scripts with QUnit, so

Where can I put test scripts and QUnit artifacts?

In the mvc project? If so, should I apparently remove these scripts through the build process when I deploy the application? It seems a bit of trash? I really don't want the test code to mix with the production code.

In a separate "test" web project? Great for better separation, but then I need some kind of build action that will move my SUT scripts to this separate project so that they can be referenced using test scripts. Option 1 is probably preferable, but still a little trash?

What is the best practice? Is there any best practice? Somehow I did not mention? Any tools that might help? Am I missing something obvious?

This proposes a separate project + Xcopy, but the answer is quite old.

Thanks.

+6
source share
2 answers

Like most people, I like to run tests separately from production code.

If you use VS2012 (or later) and the Chutzpah test adapter , you can simply create a separate class library for your tests, as well as for your .NET code.

Add the tests.js` file to the test class library and simply reference the appropriate scripts from the main project. For example, using Jasmine and Angular:

/// <reference path="../../MainProject/lib/angular/angular.js" /> /// <reference path="../../MainProject/lib/angular/angular-mocks.js" /> /// <reference path="../../MainProject/lib/angular/angular-resource.js" /> /// <reference path="../../MainProject/lib/jasmine.js" /> /// <reference path="../../MainProject/scripts/controllers.js" /> describe('My controller tests', function () { ... }); 

If you want to avoid repeating all of these reference paths in each test js file, you can add them to the _references.js file, and then just specify only one script in your tests.js file. You will need to manually link to the file because you are not in a web project, and console projects do not have the same implicit links that web projects do.

+5
source

If you have a choice, just put the tests and test infrastructure in the main code.

If you have to split up, then most people just create a separate test project that references production code (including js scripts).

0
source

Source: https://habr.com/ru/post/949268/


All Articles