2013-10-22 115 views
3

我正在嘗試對使用AngularJS編寫的應用程序執行一些端到端測試。目前,我在e2e.tests.js中有以下測試設置:使用Jasmine進行端到端測試

describe('MyApp', function() { 
    beforeEach(function() { 
     browser().navigateTo('../index.html'); 
    }); 

    it('should be true', function() { 
     expect(true).toBe(true); 
    }); 
}); 

奇怪的是,這個基本的測試失敗了。測試本身運行。但結果是:

203ms browser navigate to '../index.html' 
5ms expect undefined toBe true 
      http://localhost:81/tests/e2e.tests.js:10:9 
      expected true but was undefined 

考慮到「真」是硬編碼的,我期望它通過這個測試。我不知道真正的未定義如何。我錯過了什麼?

+0

如何在你的index.html中硬編碼「true」? – BKM

+0

[這個答案](http://stackoverflow.com/questions/13173719/is-it-possible-to-mix-testacular-karma-with-angular-scenario)應該有所幫助。 –

回答

7

有關Angular的E2E測試框架的事情是,看起來像茉莉花,但它不是茉莉花。 E2E測試代碼實際上全部是異步的,但它是以一種聰明的方式編寫的,以便調用看起來很正常。大多數調用會創建稍後測試的異步任務和Future對象。未來的目標有點像一個承諾,但有點不同。它有一個value屬性,它在準備就緒時設置,然後它調用done函數移至下一步。在E2E測試中,expect函數採用Future對象,而不是值。您看到undefined,因爲expect正在針對future.value進行測試,在這種情況下,它是true.value,這是未定義的。

嘗試使用available selectors that return futures之一,然後測試結果。事情是這樣的:

expect(element("html").text()).toMatch("Our App"); 

未來對象不會有據可查的,但你應該能夠手動創建一個未來是這樣的:

var trueFuture = angular.scenario.Future(
    "a true value",   // name that is displayed in the results 
    function(callback) {  // "behavior" function 
     callback(null, true); // supposed to call callback with (error, result) 
    }); 
expect(trueFuture).toEqual(true); 

如果您在NG-場景的源代碼看,你可以看到place where the matcher tests future.value in the angular.scenario.matcher function

+0

偉大的答案。感謝睡眠。 – SimplGy

0

我也遇到類似的問題,這是我發現的。 你必須在配置文件中使用ng-scenario作爲你的框架。 事實是ng-scenario中的expect函數不會使用任何var值或布爾值。只需要的功能,如

expect(browser().location().path()).toContain('some/string') 

或類似

expect(element('some element').html()).toContain('some String'); 

任何變量值或布爾在預期的功能是不確定的一些其他的NG-場景功能。

如果你想使用Boolean(true/false)或者你想讓你的測試通過,那麼你必須從配置文件的框架部分刪除'ng-scenario'。 試試只有茉莉花!

相關問題