我如何使用chai.js来测试我的函数是否创建了一个数组

how can I use chai.js to test if my function creates an array?

本文关键字:创建 数组 一个 是否 函数 chai 何使用 js 我的 测试      更新时间:2023-09-26

我开始学习chai.js -非常有用。我坚持理解如何使用chai来测试我的函数是否成功创建了一个数组。

这是一个通过的早期测试:

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/

(见页面末尾的bubblesort函数)

但是一些关于如何测试这个功能的建议会非常有帮助。是否有必要拥有捕获字符串并将其转换为自己的函数中的数组的功能?

谢谢! !

Chai有一些检查类型的功能。

如果你使用Assert,你可以简单地做:

assert.isArray(yourValue);

对于Expect,您应该能够执行:

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

这个答案可以更新,抱歉我还不能评论;)你的函数/方法从输入参数返回字符串数组…

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');
  });
});