jasmine spy access individual arguments passed to spy
Jasmine spies are super useful when writing unit tests
You can check arguments passed to a spy x with: expect(x).toHaveBeenCalledWith(arguments)
toHaveBeenCalledWith() expects a match on all arguments
But what if you don't know all the arguments e.g. only sure of the 1st but not the rest?
There is a way, heres the fiddle and code below:
describe("Spy argumnts access", function() {
it("should access 1st argument to spy", function() {
var mockCallback = jasmine.createSpy("mockCallback");
mockCallback("denis", 22, {unknown: 1})
expect(mockCallback).toHaveBeenCalled();
expect(mockCallback.argsForCall[0][0]).toBe("denis");
});
});
FYI Add Jasmine as an external library, I found this on other examples:
http://searls.github.io/jasmine-all/jasmine-all-min.js
You can check arguments passed to a spy x with: expect(x).toHaveBeenCalledWith(arguments)
toHaveBeenCalledWith() expects a match on all arguments
But what if you don't know all the arguments e.g. only sure of the 1st but not the rest?
There is a way, heres the fiddle and code below:
describe("Spy argumnts access", function() {
it("should access 1st argument to spy", function() {
var mockCallback = jasmine.createSpy("mockCallback");
mockCallback("denis", 22, {unknown: 1})
expect(mockCallback).toHaveBeenCalled();
expect(mockCallback.argsForCall[0][0]).toBe("denis");
});
});
FYI Add Jasmine as an external library, I found this on other examples:
http://searls.github.io/jasmine-all/jasmine-all-min.js
Comments
Post a Comment