
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