【问题标题】:How does "expect" work?“期望”如何运作?
【发布时间】:2017-09-21 10:36:39
【问题描述】:

我正在尝试使用 mocha 和 chai 创建逻辑测试

var helperFunctions = require('../lib/helperfunctions.js');
var expect = require('chai').expect;

suite('Sample logic tests',function(){
    test('getDynamicContent() should return dynamic content',function(){
       console.log(typeof helperFunctions.getDynamicContent()); // string
       expect(false); //Doesnt seem to do anything
       expect(typeof helperFunctions.getDynamicContent() === 'number'); 
    });
});

我的 helperFunctions.js:

exports.getDynamicContent = function(){

    var dynamicContentSource = [
        "Dynamic Content 1",
        "Dynamic Content 2",
        "Dynamic Content 3",
        "Dynamic Content 4",
        "Dynamic Content 5"
    ];
    var randomDynamicContent = dynamicContentSource[Math.floor(Math.random()*dynamicContentSource.length)];
    return randomDynamicContent;
};

出于某种原因,我的测试总是通过,即使 typeof helperFunctions.getDynamicContent() 是一个字符串。

谁能给我解释一下我不明白的地方?

【问题讨论】:

  • 大多数断言 API 看起来像这样:expect(actual).to.be(expected)。您没有使用任何匹配器。 chaijs.com/api/bdd

标签: javascript node.js chai


【解决方案1】:

您应该使用上述评论所说的匹配器。以expect().to.be.equal() 为例

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    相关资源
    最近更新 更多