I want to ask somthing about jasmine spy . Normally i use spy like this
function getAuthrize(id) {
$.ajax({
type: "GET",
url: "/Account/LogOn" + id,
contentType: "application/json; charset=utf-8",
dataType: "json"
});
}
spyOn($, "ajax");
getAuthrize(123);
expect($.ajax).toHaveBeenCalled();
but i want to know that what if i want to validate more things like ( the url
called in ajax call is /Account/LogOn
, type is 'Get'
and so on .
Thanks in advance
For that you need to use a fake server object
Something like sinon.fakeServer
describe('view interactions', function(){
beforeEach(function() {
this.saveResponse = this.serverResponse.someObj.POST;
this.server = sinon.fakeServer.create();
this.server.respondWith(
'POST',
this.saveResponse.url,
this.validResponse(this.saveResponse)
);
});
afterEach(function() {
this.server.restore();
});
});
Need to make sure you have the this.serverResponse
object defined
To check if a spy was called with specific parameters you an use toHaveBeenCalledWith
like this:
expect($.ajax).toHaveBeenCalled({
type: "GET",
url: "/Account/LogOn" + id,
contentType: "application/json; charset=utf-8",
dataType: "json"
});
But this will be become a very hard to read error when only one field in the JSON is wrong.
Another way is to use mostRecentCall.args
:
var args = $.ajax.mostRecentCall.args[0];
expect(args.type).toEqual('GET')
expect(args.url).toEqual('/Account/LogOn123')
This will lead in a better readable errors as you can see which paramater was wrong.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.