【发布时间】:2020-06-26 00:39:39
【问题描述】:
我是 node.js 的新手,我在为一个预期会引发错误的函数设置简单的单元测试时遇到问题。我的功能很简单:
const which_min = function(array) {
var lowest = 0;
for (var i = 1; i < array.length; i++) {
if (array[i] < array[lowest]) lowest = i;
}
return lowest;
}
我想测试我的函数在没有参数传递时是否抛出错误。在我的测试文件夹中,我有一个测试文件
var assert = require('chai').assert;
var expect = require('chai').expect;
describe('#which_min()', function() {
context('with incorrect arguments', function() {
it('errorTest', function() {
expect(function(){utils.which_min();}).to.throw(new TypeError("Cannot read property 'length' of undefined"))
})
})
})
但我发现了一个非常奇怪的错误:
AssertionError: expected [Function] to throw 'TypeError: Cannot read property \'length\' of undefined' but 'TypeError: Cannot read property \'length\' of undefined' was thrown
+ expected - actual
我真的看不出我所期望的和我得到的有什么不同 - 那么为什么我在这里没有通过测试呢?我希望它是带引号的东西?
谢谢 /基拉
【问题讨论】:
标签: node.js unit-testing testing mocha.js chai