单元测试 – Angular2 RC5如何正确配置测试模块

前端之家收集整理的这篇文章主要介绍了单元测试 – Angular2 RC5如何正确配置测试模块前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在更新Angular2 RC5的单元测试. changelog注意到以下重大变化:

addProviders [is deprecated],use TestBed.configureTestingModule instead

但是,只有在尝试在测试中包含服务时,这似乎才会出错.我的单元测试用于执行以下操作:

beforeEach(() => addProviders([
    MyService,MockBackend,... 
]));

它现在应该配置测试模块:

TestBed.configureTestingModule({
    providers: [
        StoryService,...
    ]
});

但那现在抛出一个错误

Service: MyService encountered a declaration exception Failed

Error: Cannot configure the test module when the test module has already been instantiated. Make sure you are not using inject before TestBed.configureTestingModule.

我已经检查过在configureTestingModule之前没有调用inject.这不会影响任何其他组件/指令测试,它们似乎传递得很好.如何使用RC5对服务进行单元测试来解决错误?我意识到我可能要等到RC5的测试文档更新,但是对于可能的解决方案的任何见解都将非常感激.

解决方法

在BeforeEach里面贴上TestBed.configureTestingModule,如下所示:

beforeEach(() => {
    TestBed.configureTestingModule({
        providers: [
            StoryService,...
]
    });
});

猜你在找的Angularjs相关文章