Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended structure for testing Javascript with QUnit in ASP.NET

Tags:

I have a standard ASP.NET MVC (version 2 preview 2) solution with the actual project and server-side unit tests in separate projects.

Because this project is very client-side heavy, I want to make a ClientTest project as well that uses QUnit to test the main project.

I've thought of creating a regular ASP.NET webforms project with a single HTML file that would load the various scripts in my Scripts/ directory and test them with QUnit. Unfortunately this will spawn another ASP.NET Development Server. I could configure the port of the running MVC project server before running the tests, but there's got to be a better way that isn't just throwing the test html file into the main MVC project.

Does anyone know of a better way of going about this?

like image 208
wm_eddie Avatar asked Oct 16 '09 05:10

wm_eddie


People also ask

Can we write unit test for JavaScript?

JavaScript Unit Testing is a method where JavaScript test code is written for a web page or web application module. It is then combined with HTML as an inline event handler and executed in the browser to test if all functionalities are working as desired. These unit tests are then organized in the test suite.

What is a test suite JavaScript?

A test suite is a block of unit tests that are all closely related; they test the same function or similar parts of our code base. We introduce a test suite in Mocha using describe() Each individual unit test is sometimes called a “spec” Mocha makes it natural to write specs by containing them in a function called it()


2 Answers

I like your idea of placing the QUnit tests in a separate project. What about using XCOPY to copy the scripts in the pre-build event?

Say your MVC project is MyProj.Web and your QUnit test project is MyProj.ClientTest (replace with your project names).

  • Create a Scripts folder in your ClientTest project.

  • From Project > MyProj.ClientTest Properties > Build Events, add the following to Pre-build event command line:

    XCOPY "$(SolutionDir)MyProj.Web\Scripts" "$(ProjectDir)Scripts" /S /Y

  • Then in your HTML just include the appropriate JavaScript files from the Scripts folder.

Note: You will have to rebuild your ClientTest project to refresh JavaScript files when you want to rerun tests. Adjust folder names, paths and XCOPY options as needed.

like image 188
Mike Henry Avatar answered Sep 19 '22 14:09

Mike Henry


Perhaps you could pick and choose techniques from this article, including using the command-line, harnessing NUnit with WatiN, and scraping test results for reporting. This solution wouldn't require a separate WebForms project to harness the tests in, since it's all handled by WatiN.

like image 43
G-Wiz Avatar answered Sep 21 '22 14:09

G-Wiz