Angularjs Jasmine Spy ha fornito fabbrica e immolle
-
21-12-2019 - |
Domanda
Voglio fornire la mia funzione per sostituire una fabbrica e voglio anche la possibilità di utilizzare .and.callThrough()
per utilizzare la funzionalità originale.Il problema principale che sto entrando è che non riesco a iniettare una fabbrica nel Dichiarazione del modulo Dich.
describe("It", function() {
var mockFactory;
//This works, but the original functionality is gone at this point because I'm overriding it with $provide
beforeEach(angular.mock.module('myModule', function($provide) {
mockFactory = jasmine.createSpy('myFactory');
$provide.factory('myFactory', function() { return mockFactory });
}));
//This fails because I cant inject the actual factory into the module mock
beforeEach(angular.mock.module('myModule', function($provide, myFactory) {
mockFactory = jasmine.createSpy('myFactory', myFactory);
$provide.factory('myFactory', function() { return mockFactory });
}));
})
.
Qualche idea su come superare questo?Grazie in anticipo!
Soluzione
Puoi usare il $provide.decorator()
come questo:
describe('It', function() {
var mockFactory;
beforeEach(module('myModule', function ($provide) {
$provide.decorator('myFactory', function ($delegate) {
mockFactory = jasmine.createSpy('myFactory', $delegate).and.callThrough();
return mockFactory;
});
}));
it('should call through', function () {
mockFactory('foo', 'bar');
expect(myFactory).toHaveBeenCalled();
});
});
.
Spero che questo aiuti.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow