Angular2 + Jasmine,单元测试,未找到导入测试模块-404

时间:2016-02-23 17:01:57

标签: jasmine angular

从" angular2 / testing";

导入{describe,expect,it,xit,inject,beforeEachProviders}

没有这行代码,我可以成功运行我的Jasmine简单测试代码。但是对于更复杂的代码,我需要从" angular2 / testing"

导入模块

错误:无法加载资源404.因为它在此路径中查找文件:project_folder / angular2 / testing而不是project_folder / node_modules / angular2 / testing。

这是我的test.html文件:

<head>
    <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>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script>
    System.config({
        packages: {
            'app': {defaultExtension: 'js'}
        }
    });
    System.import('app/test.spec')
            .then(window.onload)
            .catch(console.error.bind(console));
</script>
</body>

这是我的package.json:

{
  "name": "angular2-quickstart",
  "version": "1.0.0",
  "scripts": {
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "live": "live-server",
    "lite": "lite-server",
    "start": "concurrent \"npm run tsc:w\" \"npm run lite\" "
  },
  "license": "ISC",
  "dependencies": {
    "angular2": "2.0.0-beta.0",
    "systemjs": "0.19.6",
    "es6-promise": "3.0.2",
    "es6-shim": "0.33.3",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.0",
    "zone.js": "0.5.10",
    "bootstrap": "3.3.6"
  },
  "devDependencies": {
    "jasmine-core": "2.4.1",
    "lite-server": "1.3.1",
    "live-server": "^0.9.0"
  }
}

最后,我通过添加以下脚本解决了这个问题:(注意:应该在testing.dev.js之前添加system.src.js)

<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="node_modules/rxjs/bundles/Rx.js"></script>
    <script src="node_modules/angular2/bundles/angular2.dev.js"></script>
    <script src="node_modules/angular2/bundles/http.dev.js"></script>
    <script src="node_modules/es6-shim/es6-shim.js"></script>

    <script src="node_modules/angular2/bundles/testing.dev.js"></script>

1 个答案:

答案 0 :(得分:1)

您需要在HTML文件中使用test.dev.js元素添加<script>文件:

<head>
  (...)
  <script src="node_modules/angular2/bundles/test.dev.js"></script>
  (...)
</head>

angular2/testing模块已注册到此文件中......