2016-02-26 46 views
7

我在官方網站有以下角度文檔。但在文檔測試部分已經過時,並且不適用於當前的角度2測試版。我需要編寫一個基本測試來檢查條件是否正常工作。我怎麼做,在角2我們如何寫角2的基本單元測試?

+0

你能請張貼你試過的東西和失敗的地方嗎?任何錯誤消息? –

回答

0
立即

角對角2的單元測試好的文檔和端到端使用茉莉和業力測試。它使用示例來解釋它,並且很容易理解和遵循。

參考:​​

9

設置茉莉茉莉使用運行打字原稿的單元測試angular2(beta.7):

  1. 設置一個角項目
    (見5描述閔快速入門 https://angular.io/guide/quickstart

    ROOTDIR是myproject的

  2. 安裝與茉莉MPM

    npm install jasmine-core --save-dev --save-exact 
    
  3. 安裝Live服務器
    https://www.npmjs.com/package/live-server

  4. 獲取語法/ IntelliSense支持:在myproject的
    /分型做一個新的文件jasmine.d.ts

    /// <reference path="jasmine\jasmine.d.ts" /> 
    

    獲取jasmine.d.ts
    https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/jasmine/jasmine.d.ts

    並將其保存在MyProject的\分型\茉莉花作爲文件jasmine.d.ts

  5. 保存單位的test.html中的myproject

    <html> 
    <head> 
        <title>Angular2: Jasmine 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.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 file explicitly 
        System.import('app/app.spec') 
         // #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> 
    

    。然後(在window.onload)是非常重要的開始testexecution。

    看到這裏Wait for a module to be loaded to execute the tests it contains

  6. 創建目錄的myproject \應用新的文件app.spec.ts

    import {AppComponent} from './app.component'; 
    
    
    // Jasmin Test App title property 
    describe('AppComponent',() => { 
        var app: AppComponent = null 
    
        beforeEach(() => { 
        app = new AppComponent(); 
        app.title = "App in Test"; 
        }); 
    
        it('should have an title property',() => { 
    
         expect(app.title).toBeDefined(); 
        }); 
    
        it('should have the title App in Test',() => { 
    
         expect(app.title).toBe("App in Test"); 
        }) 
    }); 
    
    
    // Jasmin Test without Angular 
    describe("A suite", function() { 
        it("contains spec with an expectation", function() { 
         expect(true).toBe(true); 
        }); 
    }); 
    
  7. 從CMDLINE開始

    live-server --open=unit-test.html 
    

這是我的使用打字機打字的茉莉花運行單元測試的工作設置wi th Angular 2.

如果您有任何錯誤,請發佈您嘗試過的內容以及Günther在他的評論中提出的失敗之處。

+0

我會嘗試這個,如果它工作,我會接受答案。 –

+0

不客氣! – Marc