我一直在遵循教程https://angular.io/docs/ts/latest/guide/testing.html来使用the Tour of Heroes教程实现Angular 2的Jasmine。
当我使用"hero.spec“进行测试时,一切都很正常。但是,当我尝试使用名为"app.spec“的单元测试文件测试"app.component.ts”时,系统JS不会将"js“附加到它后面。我收到以下错误:
angular2-polyfills.js:126 GET http://127.0.0.1:8080/src/test/app.component 404 (Not Found)`
system.src.js:1068 GET http://127.0.0.1:8080/angular2/core 404 (Not Found)app.spec.ts文件如下所示:
import { AppComponent } from 'app.component';
describe('AppComponent', () => {
let app : AppComponent;
beforeEach(() => {
app = new AppComponent();
});
it('true to be true', () => {
expect(true).toEqual(true);
});
});单元测试文件如下:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Ng App Unit Tests</title>
<link rel="stylesheet" href="node_modules/jasmine-core/lib/jasmine-core/jasmine.css">
<script src="node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
<script src="node_modules/jasmine-core/lib/jasmine-core/jasmine-html.js"></script>
<script src="node_modules/jasmine-core/lib/jasmine-core/boot.js"></script>
</head>
<body>
<!-- #1. add the system.js library -->
<script src="node_modules/systemjs/dist/system-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script>
// #2. Configure systemjs to use the .js extension
// for imports from the app folder
System.config({
packages: {
'app': {defaultExtension: 'js'}
}
});
// #3. Import the spec files explicitly
Promise.all([
System.import('app/app.spec'),
System.import('app/app.component')
])
// #4. wait for all imports to load ...
// then re-execute `window.onload` which
// triggers the Jasmine test-runner start
// or explain what went wrong.
.then(window.onload)
.catch(console.error.bind(console));
</script>
</body>
</html>它看起来像是系统JS将"js“附加到"app.spec”,而不是"app.component“或其他。有谁能给我指个方向吗?
谢谢
发布于 2016-04-20 14:23:14
尝试:import { AppComponent } from './app.component',或者如果app.component在应用程序文件夹:'./app/app.component'下。等级库文件是否与零部件位于同一文件夹中?
发布于 2016-04-20 15:21:26
我想您忘记了将一些JS文件导入到您的HTML文件中,因为我看到angular2/core模块有一个404错误。此外,我认为您不需要导入您想要测试的模块(app.component),因为它将被导入到您的测试模块中。
此外,我看到您的测试文件不在app文件夹下,因为SystemJS尝试通过这个URL:http://127.0.0.1:8080/src/test/app.component加载它。您只需配置app文件夹。这就是该工具不附加js扩展名的原因。
下面是我将在HTML文件中使用的内容:
<script src="https://code.angularjs.org/2.0.0-beta.7/angular2-polyfills.js"></script>
<script src="https://code.angularjs.org/tools/system.js"></script>
<script src="https://npmcdn.com/typescript@1.8.2/lib/typescript.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.7/Rx.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.7/angular2.dev.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.7/testing.dev.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine-html.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/boot.js"></script>
<script>
System.config({
packages: {
app: {
defaultExtension: 'js'
}
}
});
System.import('angular2/src/platform/browser/browser_adapter')
.then(function(browser_adapter) { browser_adapter.BrowserDomAdapter.makeCurrent(); })
.then(function() {
System.import('app/foo.bar.spec')
.then(null, console.error.bind(console))
.then(window.onload);
})
</script>https://stackoverflow.com/questions/36735001
复制相似问题