Angular2 RC5 how to configure the test module correctly

I am updating my unit tests for Angular2 RC5. changelog notes the following violation:

addProviders [deprecated], use TestBed.configureTestingModule instead

But this seems to only accept an error when trying to include a service in a test. Where my unit test used the following:

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

Now it should configure the test module:

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

But this now causes an error

Service: MyService encountered FAILED notification exception

Error: Cannot configure the test module when the test module is already created. Make sure you do not use inject until TestBed.configureTestingModule .

I checked that inject not called before configureTestingModule . This does not affect other component / directive tests, they seem to pass fine. How can I resolve this error for unit testing a service using RC5? I understand that I may have to wait until the test documentation is updated for RC5, but any understanding of the possible solutions would be highly appreciated.

+5
source share
3 answers

Stick to TestBed.configureTestingModule inside beforeEach, for example:

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

From what I read, you only need to run the test environment once. You can reset test the module and configure the test module before each test:

 beforeEach(() => { TestBed.configureTestingModule({ declarations: [ GridComponent ] }); }); afterEach(() => { TestBed.resetTestingModule(); }); 

Preconfiguration:

 var testing = require('@angular/core/testing'); var browser = require('@angular/platform-browser-dynamic/testing'); try { testing.TestBed.initTestEnvironment(browser.BrowserDynamicTestingModule, browser.platformBrowserDynamicTesting()); } catch ($error) { console.log("test env failure" + $error); } 

After reading the code, it seems that configureTestingModule will call resetTestingModule internally, but I like to declare it after each for readability.

+4
source

I had a similar problem and fixed it by resetting the test environment:

 import {MyService} from './my.service'; import {inject, TestBed} from '@angular/core/testing/test_bed'; import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing'; describe('MyService', () => { beforeEach(() => { // Must reset the test environment before initializing it. TestBed.resetTestEnvironment(); TestBed .initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting()) .configureTestingModule({ providers: [ MyService ] }); }); it('should do something', inject([MyService], (s: MyService) => { let r = s.getResult(); expect(r.length).toBe(2); })); }); 

I'm completely nil when it comes to Angular 2, so I welcome corrections from more knowledgeable people. It does not seem to need to reset and reinitialize the test environment in each test. Unfortunately, this was the only way to make it work.

+3
source

All Articles