Reputation: 1457
I can't work out of it's possible to check what a function was called with only as a partial match.
I have another test that checks the full passed arguments, but I only want to retest when one piece of data changes.
I want to avoid putting jasmine.anything()
over and over again, so ideally want to just check for one key in the object that was passed.
The actual code:
myThing.add(
"foo",
{
aCheck : true,
anObject: {}
aString: "foo",
aBool: true // I care whether it was passed as true or false
},
{ another: "object" }
)
So my assertion is:
expect(myThing.add).toHaveBeenCalledWith(
jasmine.objectContaining({
aBool: true
})
);
or
expect(myThing.add).toHaveBeenCalledWith(
jasmine.objectContaining({
aBool: false
})
);
I could start adding all the other bits of data but it gets messy:
expect(myThing.add).toHaveBeenCalledWith(
jasmine.any(String),
jasmine.objectContaining({
aCheck : jasmine.anything(),
anObject: jasmine.any(Object)
aString: jasmine.any(String),
aBool: true
}),
jasmine.any(Object)
);
But with the amount of data in the object it makes it really hard to debug.
How can I check only partially for the object that is called as the second param?
Upvotes: 0
Views: 1151
Reputation: 18809
You can take advantage of callFake
that will be called everytime the function is called.
spyOn(myThing, 'add').and.callFake((arg1, arg2, arg3) => {
console.log('myThing.add was called');
expect(arg2.aBool).toBe(true);
});
You can also take advantage argsForCall
or mostRecentCall.args
.
http://tobyho.com/2011/12/15/jasmine-spy-cheatsheet/
Upvotes: 1