Credits: Using Grunt/Karma/Jasmine-Ajax inside your Jasmine Tests

Rated: , 0 Comments
Total visits: 317
Posted on: 29th Dec 2014

Using Grunt/Karma/Jasmine-Ajax inside your Jasmine Tests

My current client is trying to add JavaScript specs to its code base and we have decided to implement a Grunt/Karma/Jasmine solution. Like many (if not most) JavaScript code bases our code will make remote calls via jQuery to the server to Get or Post data. I wanted to be write tests which mocked the over the wire call and returned me some stubbed data. Accomplishing this took very little effort and was quite simple once I figured it out.Here is how I accomplished this1) Add the Mock-Ajax.js file to your test suite. I did this manually as the NPM package used a very old version of the .js file and it caused me issues.2) Add a reference to the mock-ajax.js file to your Karma configuration files: [ ... 'js.tests/mock-ajax.js', ... ],3) Install the mocks inside the beforeEach() method inside your spec beforeEach(function() { jasmine.Ajax.install(); });4) Create your test response you are mocking inside your specvar testResponses = { results: { success: { status: 200 Credits: Using Grunt/Karma/Jasmine-Ajax inside your Jasmine Tests


Comments
There are still no comments posted ...
Rate and post your comment


Login


Username:
Password:

Forgotten password?




MySQL Error in Query:

INSERT INTO blogsystem_user_referrals (`date`,`username`,`host`,`page`) VALUES ('05/18/2024','sulki','172.69.58.254','/sulki/post-credits-using-gruntkarmajasmineajax-inside-your-jasmine-22349.html')

Table './12scblog/blogsystem_user_referrals' is marked as crashed and should be repaired