如何使用jasmine为Angular 2测试中的每个测试用例提供相同提供程序的唯一存根?

时间:2017-11-09 17:55:03

标签: angular jasmine karma-runner angular5 angular-test

假设我在同一个描述块中有两个测试用例,需要使用相同的服务,但是每个测试用例都必须提供不同的服务存根?下面的示例将按预期提供带有serviceStub1的两个测试用例,但我需要找到一种方法来为serviceStub2提供第二种情况。我花了相当多的时间,到目前为止我迷路了。任何帮助将不胜感激,谢谢。

describe('Component: ComponentUnderTest', () => {

      const serviceStub1 = {
            method: () => value1;
      };

      const serviceStub2 = {
            method: () => value2;
      };

    beforeEach(() => {

        TestBed.configureTestingModule({
            declarations: [ComponentUnderTest],
            providers: [
                {provide: Service, useValue: serviceStub1},
            ]
        });

        fixture = TestBed.createComponent(ComponentUnderTest);
    });

    //should be tested with serviceStub1
    it("should do something when provider method returns value 1");

    //should be tested with serviceStub2
    it("should do something when provider method returns value 2");
});

0 个答案:

没有答案