前提:我完全清楚服务和工厂是单身人士。
如何为每次测试重置注入的工厂?
这是我的示例代码:
describe('Test', () => {
beforeEach(module('app'));
var $scope: Registration.IRegistrationCardsScope;
var workflow : Workflow.Registration;
beforeEach(inject(($injector) => {
$scope = $injector.get('$rootScope').$new();
//injecting with angular, this should be a different factory each time, not a singleton
workflow = $injector.get("registrationWorkflow");
//actual workaround thanks to typesript, reinitializing factory manually instead of injecting
//workflow = new Workflow.Registration();
}));
it('test factory 1', () => {
workflow.getVariable(); // returns undefined <-- correct;
workflow.setVariable(1);
});
it('test factory 2', () => {
workflow.getVariable(); // returns 1 <-- wrong;
workflow.setVariable(1);
});
})
感谢任何提示!