我为一个AngularJS服务编写了以下测试。当我同时运行它们时,第一个测试。'should exist'
,但其余的都失败了,出现以下错误。
Error: [$injector:unpr] Unknown provider: aProcessorProvider <- aProcessor
我的测试:
describe('MyService', () => {
let a: any, b: any, c: any;
beforeEach(angular.mock.module('myModule'));
beforeEach(inject( (_a_, _b_) => {
a = _a_;
b = _b_;
c = b.getProperty('foo');
}));
afterEach( () => {
a = null;
b = null;
c = null;
});
//passes
it('should exist', () => {
expect(a).toBeDefined();
expect(b).toBeDefined();
expect(c).toBeDefined();
});
//all fail
describe('when something happens', () => {
it('should be a frog', () => {
let isFrog: boolean = a.isFrog();
expect(isFrog).toBeTruthy();
});
it('should not be a moose', () => {
let isMoose: boolean = b.isMoose();
expect(isMoose).toBeFalsy();
});
it('should have no soul', () => {
let soul: any = c.getSoul();
expect(soul).toBeNull();
});
});
});
当我单独运行这些测试时,它们都通过了 但当我一次运行所有的测试时,最后三个都失败了。有谁知道为什么会出现这种情况?
你需要将对 angular.mock.module()
在一个函数中,所以它在每次测试前都会运行。
beforeEach(() => {
angular.mock.module('myModule');
});