为什么我的第一个测试通过了,而其他的测试却失败了?

问题描述 投票:0回答:1

我为一个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();
        });
    });

});

当我单独运行这些测试时,它们都通过了 但当我一次运行所有的测试时,最后三个都失败了。有谁知道为什么会出现这种情况?

angularjs karma-jasmine
1个回答
0
投票

你需要将对 angular.mock.module() 在一个函数中,所以它在每次测试前都会运行。

beforeEach(() => {
    angular.mock.module('myModule');
});
© www.soinside.com 2019 - 2024. All rights reserved.