2015-02-05 129 views
4

下面的代碼片段來自angular's documentation。這裏做的it()函數是什麼(我假設它有一些常規含義,否則沒有上下文似乎給出它的含義)?我沒有在angular的網站上看到它的任何提及。由於它的名字,它也很難谷歌。上下文是關於代碼測試。這裏做的it()函數是什麼?

it('should say hello', function() { 
    var scopeMock = {}; 
    var cntl = new MyController(scopeMock); 

    // Assert that username is pre-filled 
    expect(scopeMock.username).toEqual('World'); 

    // Assert that we read new username and greet 
    scopeMock.username = 'angular'; 
    scopeMock.sayHello(); 
    expect(scopeMock.greeting).toEqual('Hello angular!'); 
}); 
+1

這是用於在[茉莉測試框架]定義測試(http://jasmine.github.io/2.2/introduction的功能之一。 HTML)和其他類似的。 – 2015-02-05 20:18:37

+0

'it','expect'和'.toEqual'這裏來自一些測試套件,而不是Angular。 – Bergi 2015-02-05 20:18:54

回答

5

的()函數是由茉莉測試框架中定義的,它不是一部分角度本身。你會在angular的文檔中看到它,因爲它們鼓勵你(有充分的理由)養成爲你的代碼編寫測試的習慣,並且展示代碼在測試中的工作方式。它()函數定義了一個茉莉花測試。它的名字是因爲它的名字使閱讀測試幾乎像閱讀英文。 it()函數的第二個參數本身就是一個函數,它在執行時可能會運行一些expect()函數。 expect()函數用於實際測試你所期望的事情是真實的。

瞭解更多關於茉莉框架的網站茉莉測試:http://jasmine.github.io/