我在官方網站有以下角度文檔。但在文檔測試部分已經過時,並且不適用於當前的角度2測試版。我需要編寫一個基本測試來檢查條件是否正常工作。我怎麼做,在角2我們如何寫角2的基本單元測試?
回答
角對角2的單元測試好的文檔和端到端使用茉莉和業力測試。它使用示例來解釋它,並且很容易理解和遵循。
參考:
設置茉莉茉莉使用運行打字原稿的單元測試angular2(beta.7):
設置一個角項目
(見5描述閔快速入門 https://angular.io/guide/quickstart)ROOTDIR是myproject的
安裝與茉莉MPM
npm install jasmine-core --save-dev --save-exact
獲取語法/ 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
保存單位的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
創建目錄的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); }); });
從CMDLINE開始
live-server --open=unit-test.html
這是我的使用打字機打字的茉莉花運行單元測試的工作設置wi th Angular 2.
如果您有任何錯誤,請發佈您嘗試過的內容以及Günther在他的評論中提出的失敗之處。
我會嘗試這個,如果它工作,我會接受答案。 –
不客氣! – Marc
I found this to be a good guide for angular testing
它雖然缺少以下命令:
typings install jasmine --save-dev --ambient
- 1. 角度單元測試:如何運行基本測試
- 2. 基本單元測試
- 3. 如何寫測試單元
- 4. 我如何編寫測試併發性的Tornado單元測試
- 5. 角2 - 單元測試諜照不叫
- 6. 單元測試通信角2
- 7. 角2單元測試用例錯誤
- 8. 我們如何從腳本中運行城市單元測試?
- 9. 角4管單元測試不是基本的管道工作
- 10. Android單元測試:我如何測試?
- 11. Symfony 2的本地化單元測試
- 12. 如何寫單元測試的輸出?
- 13. 如何編寫getPopularItems()的單元測試?
- 14. 如何角2個測試
- 15. 單元測試用例寫作的基本規則
- 16. 如何單元測試角度擴展
- 17. 我該如何寫我的流星方法的單元測試?
- 18. 單元測試基本工廠,使用與$角窗口
- 19. 如何單元測試ejb 2類
- 20. 單元測試基本控制器
- 21. 單元測試基本驗證
- 22. 基本MVC3單元測試失敗UpdateModel()
- 23. 如何編寫單元測試?
- 24. vue karma,如何編寫單元測試
- 25. 如何編寫加密單元測試?
- 26. 我們如何擁有單元測試的哲學?
- 27. 我們如何添加單元測試的JavaScript hasOwnProperty()函數
- 28. 我們如何執行DOM操作的單元測試?
- 29. 我們如何連接Doppl中的轉換單元測試?
- 30. 如何將角度控制器注入我的單元測試
你能請張貼你試過的東西和失敗的地方嗎?任何錯誤消息? –