I'm using Jasmine 2.8 with the jasmine-ajax plugin mock a jQuery getJSON call. The callback is supposed to store the data back in the object. I have a class structure as follows.
// --------- settings.js ------------
class settings{
load(filename, callback){
let self = this;
$.getJSON(filename,funciton(d){
let err = false;
self._d = d;
callback(d, err);
}).fail(function(jqxhr, stat, err){
let msg = ... build error message ...
callback(msg, true);
});
}
}
// ------- myModule.js --------------
var Settings = require('settings');
class myModule{
constructor(settingsFile){
this._d = {};
this._file = settingsFile;
this._settings = new Settings();
}
load(callback){
let self = this;
this._settings.load(this._file, function(d,err){
if(!err){
self._d = d;
console.log(self._d); // prints the object
callback('success');
}
});
}
}
// ---------- myModuleSpec.js -------------
describe('manipulating data data', function(){
beforeEach(function(){
this.d = { pro1:'property1', prop2:{ foo:'bar' }};
jasmine.Ajax.install();
this.my = new MyModule('myfile.json');
this.cb = new jasmine.createSpy();
this.my.load(this.cb);
let r = jasmine.Ajax.requests.mostRecent();
r.respondWith({
status:200,
responseText.JSON.stringify(this.d);
});
});
afterEach(function(){
this.my = undefined;
jasmine.Ajax.uninstall();
});
it('should have called the callback',function(){
expect(this.cb).toHaveBeenCalled(); //passes
});
it('should contain the data', function(){
console.log(this.my._d); // prints { } to the console
expect(this.my._d).toEqual(jasmine.objectContaining(this.d)); // FAIL
});
});
Jasmine returns the error message: Expected({ }) to equal .
Putting in print statements into the code verifies the callback as been called and received the but when the test is run the data property (_d) is an empty object.
I'm using jasmine on the command line within nodejs. I have a helper which loads a virtual dom using JSDOM and configures global.getJasmineRequireObj to configure jaxmine-ajax;
Thank you.