如何使用 Chai 测试函数的 return 值
How to test return values on functions with Chai
我正在使用 Chai + mocha + karma 来测试我的 js...
我有一个简单的函数,它将一个数字加 2:
function name(str) {
return str + ' has come online';
}
我收到断言错误,AssertionError: expected [Function: add] to be a string
但我不确定为什么,因为它是一个字符串...
describe("Number", function() {
it("Should return a string value", function() {
expect(name).to.be.a('string');
})
});
测试正在抛出错误,因为它正在检查函数 name
本身,而不是调用函数的结果。你需要做:
expect(name('something')).to.be.a('string');
如果您正在使用 assert
,您可以这样做:
assert.typeOf(name('someting'),'boolean');
您可以查看documentation了解更多详情
我正在使用 Chai + mocha + karma 来测试我的 js...
我有一个简单的函数,它将一个数字加 2:
function name(str) {
return str + ' has come online';
}
我收到断言错误,AssertionError: expected [Function: add] to be a string
但我不确定为什么,因为它是一个字符串...
describe("Number", function() {
it("Should return a string value", function() {
expect(name).to.be.a('string');
})
});
测试正在抛出错误,因为它正在检查函数 name
本身,而不是调用函数的结果。你需要做:
expect(name('something')).to.be.a('string');
如果您正在使用 assert
,您可以这样做:
assert.typeOf(name('someting'),'boolean');
您可以查看documentation了解更多详情