2015-09-17 68 views
0

我開始學習chai.js - 非常有用。我堅持瞭解如何使用chai來測試我的函數是否成功創建了一個數組。如何使用chai.js來測試我的函數是否創建一個數組?

這是一個早期的測試通過:

describe('wordSearch', function() { 
    it("takes an input and returns a changed input to the screen", function() { 
    expect(wordSearch("hello world", "world", "hello universe")).to.equal("hello universe"); 
    }); 
}) 

,我發現一些例子在這裏:http://www.andrewsouthpaw.com/2015/01/08/beginners-guide-to-testing-with-mocha-chai/

(看他的冒泡排序功能,實現頁面的末尾)

但一些瞭解我如何測試這個功能的指針會非常有幫助。是否有必要具有捕獲一個字符串並將其轉換爲一個數組的功能?

謝謝!

回答

1

Chai有一些檢查類型的函數。

如果你使用斷言,你可以簡單地做:

assert.isArray(yourValue); 

對於期待你應該能夠做到:

expect(yourValue).to.be.an('array'); 
0

這個答案可以更新,對不起,我不能發表評論之中; ) 如果我理解你corectly你的函數/方法從輸入參數返回一串字符串...

describe('wordSearch', function() { 
    it('takes an input and returns a changed input to the screen', function() { 
    var output = wordSearch('hello world', 'world', 'hello universe'); 
    expect(output).to.be.an('array'); 
    expect(output).to.include('hello universe'); 
    }); 
}); 
相關問題