Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript test structuring in .NET / Visual Studio

I have a .NET MVC solution, it contains various javascripts, I want to be able to test those scripts using QUnit, so

Where do I put the test scripts and QUnit artefacts?

In the mvc project? If yes, I then, presumably, need to remove these scripts via a build process when I deploy the application? Seems a bit rubbish? I really don't want test code mixed in with production code.

In a separate "test" web project? Great for better separation, but then I need some build action that will move my SUT scripts to this separate project so they can be referenced by test scripts. Probably preferable to option 1, but still a bit rubbish?

What's the best practice? Is there a best practice? Some other way I haven't mentioned? Any tools that can help? Have I missed something obvious?

This suggests the separate project + Xcopy type solution, but the answer is pretty old.

Thanks.

like image 580
Greg Smith Avatar asked Jul 11 '13 19:07

Greg Smith


1 Answers

Like most people I like to keep tests separate from production code.

If you're using VS2012 (or later) and the Chutzpah test adapter then you can simply create a separate class library for your tests, much like you would for your .NET code.

Add a 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 those reference paths in each test js file you have, you can add them to a _references.js file and then just reference that one script in your tests.js file. You'll need to manually reference the file because you're not in a web project and console projects don't have the same implicit references that web projects do.

like image 83
Richard Banks Avatar answered Oct 23 '22 09:10

Richard Banks