Load Local Json Into Jasmine/karma Unit Test In Angularjs
I'm testing a callback function which accepts a response object as it's only parameter. This object is the response of a HTTP request made elsewhere so I don't want to user $httpBa
Solution 1:
@dean-sherwin Though this might not actually answer the question, I'd like to share the piece of code that I've been using to load the json from a file for jasmine testing because:
- not always do you want to keep the huge json data in one test spec
- helps in sharing that json data across multiple specs if need be
spyOn(SomeClass, 'someMethod').and.returnValue(
$.ajax({
url: "somefolder/anotherfolder/theJSONFile.json",
async: false,
dataType: "json"
}).then(function(data) {
return data
})
);
Solution 2:
You can do it like this:
var LoginService, $controller;
var formFetcherService = {
updateCachedForms: jasmine.createSpy('sessionData');
}
var response = {
data: {
user_id: 4,
forms: 'some'
}
}
beforeEach(module(function($provide) {
$provide.value('LoginService', {
login: function(url, username, password, successCallback, errorCallback) {
successCallback(response);
}
});
$provide.value('FormFetcherService', formFetcherService);
}))
beforeEach(inject(function(_$controller_) {
$controller = _$controller_;
});
it('should create sessionData', function() {
var controller = $controller('HomeController');
controller.submitLogin();
expect(formFetcherService.updateCachedForms).toHaveBeenCalledWith(response.user_id, response.form_uri);
});
Post a Comment for "Load Local Json Into Jasmine/karma Unit Test In Angularjs"