I've been trying to mock a provider (for use in another provider) and couldn't manage to get it mocked. In the test, I see the actual provider function and not the mocked one.
Here is the (simplified) code:
First, the setup: (I removed anything that isn't needed):
function ServiceToMock() {
this.functionMock = function () {
// do something...
};
this.$get = function () {
// doesn't matter
};
}
module('theModule').provider('serviceToMock', ServiceToMock);
function ServiceThatUsesMock(serviceToMock) {
serviceToMock.functionMock(); // I want the mocked function to run here but the actual one does
this.$get = function () {
// doesn't matter...
};
}
module('theModule').provider('serviceThatUsesMock', ServiceThatUsesMock);
As you can see, two services defined with .provider(), the second is dependent on the first.
And the unit test:
let functionMock = jasmine.createSpy();
module('theModule');
module(function ($provide) {
$provide.provider('serviceToMock', function () {
this.functionMock = functionMock;
this.$get = () => ({});
this.test = 'aaa';
});
});
var serviceThatUsesMock;
beforeEach(inject(function (_serviceThatUsesMock_) {
serviceThatUsesMock = _serviceThatUsesMock_;
}));
it('should register for update failure', function () {
expect(functionMock).toHaveBeenCalled();
});
The test fails because the real function gets called and not the mocked one.
What Am I Doing wrong?
Thanks!
Aucun commentaire:
Enregistrer un commentaire