Reputation: 14250
I have something in my code like this.
$scope.callMe = function(test){
...
myService.test(
{'price':price, 'name':name, 'category': category, 'test': test}
) // category, price are complex objec
...
}
my unit test
it('should check the params', function(){
spyOn(myService.test).and.callThrough();
$scope.callMe('test1');
expect(myService).toHaveBeenCalled();
}
I want to create a unit test to see if myService
is called with a variable 'test1'
. I don't want to use toHaveBeenCalledWith()
because I have to mimic bunch of complex objects and I don't want to do that. The above test won't do it. Is there a way to solve it? Thanks a lot!
Upvotes: 0
Views: 52
Reputation: 222319
Mimicking a bunch of complex objects is a part of unit testing. The whole concept of fixtures is dedicated to that.
Separating data from the specs is not uncommon, it is convenient to serialize existing objects instead of creating fakes from scratch.
var someServiceFixture = require('./fixtures/someService.json');
...
expect(myService.test).toHaveBeenCalledWith(someServiceFixture);
When some arguments are positively irrelevant, fixtures can be skipped in favour of loose argument checks with :
expect(myService.test).toHaveBeenCalledWith({'price': jasmine.any(Object), ...);
In controller unit tests everything but controller should be mocked, only one unit is tested at time. This is most appropriate scenario in current spec. The controller doesn't have to satisfy stubbed service with valid arguments:
spyOn(myService, 'test'); // should be stubbed
$scope.price = jasmine.createSpy('price'); // should be exposed to scope to be mocked
$scope.callMe('test1');
expect(myService.test).toHaveBeenCalledWith({'price': $scope.price, ...);
toHaveBeenCalledWith
performs deep equality check on objects, {}
dummy objects comparison may be ineffective. if controller code doesn't depend on price
type, a spy (or any other dummy function) can be used instead of object argument to make it compared strictly.
Upvotes: 1