且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-15 13:09:52

据我所读,您只需初始化测试环境一次.然后,您可以重置测试模块并在每次测试之前配置测试模块:

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

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

    afterEach(() => {
        TestBed.resetTestingModule();
    });

前期配置是:

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);
}

通读代码后,configureTestingModule似乎会在内部调用resetTestingModule,但出于可读性考虑,我希望在每次声明之后都将其声明.

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