测试Angular服务

时间:2018-01-16 20:03:54

标签: angular unit-testing jasmine

我正在尝试测试服务并且设置有困难。我的初始设置是

  beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [ServiceWeAreTesting,,
                { provide: DependencyOne, useClass: MockDependencyOne },
                { provide: DependencyTwo, useClass: MockDependencyTwo },
                { provide: DependencyThree, useClass: MockDependcyThree },
            ]
        });
    });

 beforeEach(() => {
        serviceTest = TestBed.get(ServiceWeAreTesting);
        spyOn(serviceTest, "testFuncOne").and.callThrough();
        spyOn(serviceTest, "testFuncTwo").and.callThrough();
      });

我试图在serviceTest中测试 - testFuncOne,正在调用testFuncTwo。但是这些函数在构造函数中被调用,所以我认为问题是spyOn设置得太迟了。我该如何设置间谍?

编辑:

beforeEach(() => {
        mockDependencyThree = TestBed.get(DependencyThree)
        spyOn(mockDependencyThree, "function").and.callThrough();
        serviceTest = TestBed.get(ServiceWeAreTesting);
        spyOn(serviceTest, "testFuncOne").and.callThrough();
        spyOn(serviceTest, "testFuncTwo").and.callThrough();
      });

然后在我的测试中我正在做

  it('mockDependencyThree function call test', () => {
        expect(mockDependencyThree.function).toHaveBeenCalled();
    })

创建原始服务时应该调用该函数。

0 个答案:

没有答案