Skip to main content

Posts

Showing posts from July, 2018

Using MockJax with QUnit

In this post, it is shown how the JQuery  MockJax  plugin can be used to mock an Ajax request within a QUnit test. The content seen here is an extension of the work shown in the  JavaScript Unit Testing with QUnit  post. Below is the user.js   file containing the getAllUsers function which calls the GetAllUsers  method on the API. This is the method that we want to mock in the new unit test. The QUnit test is a bit more complex compared to the one written in the previous post. Firstly, the variable done  is set to assert.async()  which pauses the processing of the test until done() is called. Next, the mock is setup for the "api/GetAllUsers" Ajax request using Mjaxockjax. We define the url, content type, the type of request then finally the response that will be generated when the request is called. The getAllUsers function is called and the returned data  is passed into an assert.deepEqual , along with the expected result ...

JavaScript Unit Testing with QUnit

In earlier blog posts I have explained the ways in which server side code can be unit tested using MSpec and Specflow. This post details how JavaScript code can be unit tested in a similar fashion, however this time utilising QUnit . A simple calculator class is used an example for this post. Below is the JavaScript file that will be tested by a QUnit test. The file is named calculator.js  and contains a single function that takes two numbers as arguments and returns the summation. The test is simple to write. The QUnit.test  takes a name (displayed in the browser when the tests are run) before the assertion is made. In this case an assert.ok   is used but there are many possible assertions that can be used which are listed here . (In a later post I will detail how assert.deepEqual   can be used to test methods that return JSON). Within the assertion, the expected equality is passed in before the success message that will be displayed. Final...