给定一个组件MyComponent
出于演示目的公开一个简单的 API,并导入我们进行常规组件测试所需的符号:
import MyComponent from './my.component';
import {
describe,
expect,
it,
inject,
beforeEach,
beforeEachProviders } from '@angular/core/testing';
import { TestComponentBuilder } from '@angular/compiler/testing';
我尝试进行一个基本的测试,注入和实例化 TestComponentBuilder 以创建MyComponent
类型的夹具组件,如下所示:
describe('MyComponent', () => {
let testComponentBuilder: TestComponentBuilder;
beforeEachProviders(() => [TestComponentBuilder]);
beforeEach(inject([TestComponentBuilder], (_testComponentBuilder: TestComponentBuilder) => {
testComponentBuilder = _testComponentBuilder;
}
));
it('should feature this or that behavior', done => {
// We create a test component fixture on runtime out from the component symbol
testComponentBuilder.createAsync(MyComponent).then(componentFixture => {
// Here we proceed to run assertions on the componentFixture
// ...
done();
})
.catch(e => done.fail(e));
});
将我的代码升级到 RC.1 后,我总是遇到同样的错误。
Error: No precompiled component MyComponent found
基本上testComponentBuilder.createAsync(MyComponent)
返回的承诺永远不会得到解决。 这个问题在我所有的测试中都是一致的,包括使用TestComponentBuilder
。
作为记录,在进行任何测试之前,我之前像这样配置了测试提供程序:
import { setBaseTestProviders } from '@angular/core/testing';
import {
TEST_BROWSER_STATIC_PLATFORM_PROVIDERS,
TEST_BROWSER_STATIC_APPLICATION_PROVIDERS
} from '@angular/platform-browser/testing';
setBaseTestProviders(
TEST_BROWSER_STATIC_PLATFORM_PROVIDERS,
TEST_BROWSER_STATIC_APPLICATION_PROVIDERS
);
我已经对代码进行了高低扫描,以确定这是 RC.1 中的实际错误还是我的测试设置的问题,但没有成功。 在 RC.1 之前,我的测试设置可以在所有规格测试组件中无缝运行。
任何指针?
我设法自己解决了这个问题。 事实证明我的应用程序和平台测试提供程序设置是错误的。 像这样配置安装脚本可以解决问题:
import { resetBaseTestProviders, setBaseTestProviders } from '@angular/core/testing';
import { BROWSER_APP_DYNAMIC_PROVIDERS } from "@angular/platform-browser-dynamic";
import {
TEST_BROWSER_STATIC_PLATFORM_PROVIDERS,
ADDITIONAL_TEST_BROWSER_PROVIDERS
} from '@angular/platform-browser/testing';
resetBaseTestProviders();
setBaseTestProviders(
TEST_BROWSER_STATIC_PLATFORM_PROVIDERS,
[
BROWSER_APP_DYNAMIC_PROVIDERS,
ADDITIONAL_TEST_BROWSER_PROVIDERS
]
);
我的错! ;)
@deeleman我在使用 ng-cli 和https://github.com/juliemr/ng2-test-seed/blob/master/karma-test-shim.js使用的默认设置时遇到了同样的问题: (
/*global jasmine, __karma__, window*/
Error.stackTraceLimit = Infinity;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;
__karma__.loaded = function () {
};
var distPath = '/base/.test/';
var appPath = distPath + 'app/';
function isJsFile(path) {
return path.slice(-3) == '.js';
}
function isSpecFile(path) {
return path.slice(-8) == '.spec.js';
}
function isAppFile(path) {
return isJsFile(path) && (path.substr(0, appPath.length) == appPath);
}
var allSpecFiles = Object.keys(window.__karma__.files)
.filter(isSpecFile)
.filter(isAppFile);
// Load our SystemJS configuration.
System.config({
baseURL: distPath,
});
System.import('system.config.js').then(function(e) {
// Load and configure the TestComponentBuilder.
return Promise.all([
System.import('@angular/core/testing'),
System.import('@angular/platform-browser-dynamic/testing')
]).then(function (providers) {
var testing = providers[0];
var testingBrowser = providers[1];
testing.setBaseTestProviders(
testingBrowser.TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
testingBrowser.TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS
);
});
}).then(function() {
return Promise.all(
allSpecFiles.map(function (moduleName) {
return System.import(moduleName);
}));
}).then(__karma__.start, __karma__.error);
最有用的评论
我设法自己解决了这个问题。 事实证明我的应用程序和平台测试提供程序设置是错误的。 像这样配置安装脚本可以解决问题:
我的错! ;)