I work on a large Angular App and initially we done a lot of our tests by using $provide to mock services. However we now have a lot of Jasmine Spies in our tests in order to stub and spy on services.
i.e
spyOn(myService, 'myMethod').andReturn 'myValue'
Should we really be using $provide for this or are there cases where spying on a service is the best approach?
In the Angular Tests they use spies for spying on Jquery which I would see as an external service.
spyOn(jq.prototype, 'on');
$provide seems to be used more for internal services.
module(function($provide){
$provide.provider('$exceptionHandler', $ExceptionHandlerProvider);
});
There is also a Jasmine createSpy function but now I'm thinking that $provide should always take precedence over that.
Any insights or help in this would be appreciated.
Jasmine spies are used to track or stub functions or methods. Spies are a way to check if a function was called or to provide a custom return value. We can use spies to test components that depend on service and avoid actually calling the service's methods to get a value.
spyOn() is inbuilt into the Jasmine library which allows you to spy on a definite piece of code.
Testing Angular Applications A spy is a function that invisibly wraps a method and lets you control what values it returns or monitor how it was called. A test uses a spy to measure if a method was called, how many times it was called, and with what arguments.
callThrough()Tell the spy to call through to the real implementation when invoked.
From my own (limited) experience, I would say do whatever approach makes:
Usually the spyOn
approach works when, in order to do the above, I would like to stub a single method from a service / factory. If I need to mock an entire service / factory, then use $provide
.
A few specific cases come to mind that require one or the other:
If you're testing a service, then to stub other methods from that service, you'll have to use spyOn
To ensure that extra dependencies aren't introduced later in the code under test, than $provide
adds a bit more protection. Say, if you want to ensure that ServiceA
only requires myMethod
from ServiceB
, then $provide
I think would be the way to go, as if ServiceA
calls any undefined methods from ServiceB
during the test, errors would be raised.
$provide.provider('ServiceB', {
myMethod: function() {}
});
If you want to mock a factory that returns a function, so:
app.factory('myFactory', function() {
return function(option) {
// Do something here
}
});
Which is used as:
myFactory(option);
Then to verify that some code calls myFactory(option)
I think there is no alternative then to use $provide
to mock the factory.
Just by the way, they're not mutually-exclusive options. You can use $provide
and then still have spies involved. In the previous example, if you want to verify the factory was called with an option, you might have to:
var myFactorySpy = jasmine.createSpy();
$provide.provider('myFactory', myFactorySpy);
And then in the test at the appropriate point:
expect(myFactorySpy).toHaveBeenCalledWith(option);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With