2016-07-06 134 views
0

Testing Guide
我一直在關注它。Angular2單元測試設置問題

注:我有我的應用程序運行沒有測試部分。

問題:我無法加載unit-tests.html。我認爲問題非常小,但無法弄清楚。我正在使用VisualCode,當我點擊F5它只運行我的應用程序。那麼如何加載unit-tests.html

我的設置如下。

的package.json

{ 
    "name": "angular2-quickstart", 
    "version": "1.0.0", 


    "scripts": { 

    "test": "live-server --open=unit-tests.html", 
    //<------ I have added this as mentioned. 

    "start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ", 
    //<------ I tried to disable this line as well. 

    "lite": "lite-server", 
    "postinstall": "typings install", 
    "tsc": "tsc", 
    "tsc:w": "tsc -w", 
    "typings": "typings" 
    }, 


    "license": "ISC", 
    "dependencies": { 
    "@angular/common": "2.0.0-rc.4", 
    "@angular/compiler": "2.0.0-rc.4", 
    "@angular/core": "2.0.0-rc.4", 
    "@angular/forms": "0.2.0", 
    "@angular/http": "2.0.0-rc.4", 
    "@angular/platform-browser": "2.0.0-rc.4", 
    "@angular/platform-browser-dynamic": "2.0.0-rc.4", 
    "@angular/router": "3.0.0-beta.1", 
    "@angular/router-deprecated": "2.0.0-rc.2", 
    "@angular/upgrade": "2.0.0-rc.4", 
    "systemjs": "0.19.27", 
    "core-js": "^2.4.0", 
    "reflect-metadata": "^0.1.3", 
    "rxjs": "5.0.0-beta.6", 
    "zone.js": "^0.6.12", 
    "angular2-in-memory-web-api": "0.0.14", 
    "bootstrap": "^3.3.6" 
    }, 
    "devDependencies": { 
    "concurrently": "^2.0.0", 
    "jasmine-core": "2.4.1", 
    "lite-server": "^2.2.0", 
    "typescript": "^1.8.10", 
    "typings": "^1.0.4" 
    } 
} 

單元tests.html

<!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> 

    <script src="../node_modules/systemjs/dist/system.src.js"></script> 

    <script> 
    System.config({ 
     packages: { 
     'app': {defaultExtension: 'js'} 
     } 
    }); 

    System.import('app/app.spec') 
     .then(window.onload) 
     .catch(console.error.bind(console)); 
    </script> 

</body> 
</html> 

app.spec.ts(注:內app文件夾app.spec .ts有)

it('true is true',() => expect(true).toEqual(true)); 
+0

你可以只打開一個瀏覽器,瀏覽到本地主機':PORTNUMBER/unit-tests.html'用端口live-server啓動時替換PORTNUMBER。或者打開npm,使用'cd'導航到您的項目文件夾,然後鍵入'npm run test' – Colum

+0

是的。但是我想'system.js'來加載'testing file'。 – micronyks

+0

這樣它也說,無法獲得'app/app.spec' – micronyks

回答

0

我單位tests.html文件:

<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="content-type" content="text/html;charset=utf-8"> 
    <title>Angular2 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> 

    <script src="../node_modules/reflect-metadata/Reflect.js"></script> 
    <script src="../node_modules/zone.js/dist/zone.js"></script> 
</head> 
<body> 
    <!-- #1. add the system.js library --> 
    <script src="../node_modules/systemjs/dist/system.src.js"></script> 

    <script src="systemjs.config.js"></script> 

    <!--<script src="systemjs.builder.js"></script>--> 

    <script> 

    var fileList = ['sanity.spec']; 

    System.baseURL = 'bin/tests/'; 

    multiImport(fileList) 
    .then(window.onload) 
    .catch(console.error.bind(console)); 


    function multiImport(modules) { 
     return Promise.all(modules.map(function(m) { return System.import(m) })) 
    } 

    </script> 
</body> 
</html> 

這使得它容易分辨系統JS加載多個文件