Chai - 期望函数抛出错误
Chai - expect function to throw error
我对 Chai 很陌生,所以我仍然在掌握一些东西。
我已经编写了函数来检查 API 响应和 return 正确的消息或抛出错误。
networkDataHelper.prototype.formatPostcodeStatus = function(postcodeStatus) {
if (postcodeStatus.hasOwnProperty("errorCode")) {
//errorCode should always be "INVALID_POSTCODE"
throw Error(postcodeStatus.errorCode);
}
if (postcodeStatus.hasOwnProperty("lori")) {
return "There appears to be a problem in your area. " + postcodeStatus.lori.message;
}
else if (postcodeStatus.maintenance !== null) {
return postcodeStatus.maintenance.bodytext;
}
else {
return "There are currently no outages in your area.";
}
};
我已经设法为消息传递编写了测试,但是,我在错误测试方面遇到了困难。这是我迄今为止所写的内容:
var networkDataHelper = require('../network_data_helper.js');
describe('networkDataHelper', function() {
var subject = new networkDataHelper();
var postcode;
describe('#formatPostcodeStatus', function() {
var status = {
"locationValue":"SL66DY",
"error":false,
"maintenance":null,
};
context('a request with an incorrect postcode', function() {
it('throws an error', function() {
status.errorCode = "INVALID_POSTCODE";
expect(subject.formatPostcodeStatus(status)).to.throw(Error);
});
});
});
});
当我运行上面的测试时,我得到以下错误信息:
1) networkDataHelper #formatPostcodeStatus a request with an incorrect postcode throws an error: Error: INVALID_POSTCODE
似乎抛出的错误导致测试失败,但我不太确定。有人有什么想法吗?
请注意,我不是 Chai 专家,您拥有的构造:
expect(subject.formatPostcodeStatus(status)).to.throw(Error);
不可能在 Chai 框架开始查看您的 .to.throw()
链之前处理抛出的异常。上面的代码 调用 函数 在 调用 expect()
之前,所以异常发生得太快了。
相反,您应该将函数传递给 expect()
:
expect(function() { subject.formatPostCodeStatus(status); })
.to.throw(Error);
这样,框架可以在为异常准备好后调用函数。
我对 Chai 很陌生,所以我仍然在掌握一些东西。
我已经编写了函数来检查 API 响应和 return 正确的消息或抛出错误。
networkDataHelper.prototype.formatPostcodeStatus = function(postcodeStatus) {
if (postcodeStatus.hasOwnProperty("errorCode")) {
//errorCode should always be "INVALID_POSTCODE"
throw Error(postcodeStatus.errorCode);
}
if (postcodeStatus.hasOwnProperty("lori")) {
return "There appears to be a problem in your area. " + postcodeStatus.lori.message;
}
else if (postcodeStatus.maintenance !== null) {
return postcodeStatus.maintenance.bodytext;
}
else {
return "There are currently no outages in your area.";
}
};
我已经设法为消息传递编写了测试,但是,我在错误测试方面遇到了困难。这是我迄今为止所写的内容:
var networkDataHelper = require('../network_data_helper.js');
describe('networkDataHelper', function() {
var subject = new networkDataHelper();
var postcode;
describe('#formatPostcodeStatus', function() {
var status = {
"locationValue":"SL66DY",
"error":false,
"maintenance":null,
};
context('a request with an incorrect postcode', function() {
it('throws an error', function() {
status.errorCode = "INVALID_POSTCODE";
expect(subject.formatPostcodeStatus(status)).to.throw(Error);
});
});
});
});
当我运行上面的测试时,我得到以下错误信息:
1) networkDataHelper #formatPostcodeStatus a request with an incorrect postcode throws an error: Error: INVALID_POSTCODE
似乎抛出的错误导致测试失败,但我不太确定。有人有什么想法吗?
请注意,我不是 Chai 专家,您拥有的构造:
expect(subject.formatPostcodeStatus(status)).to.throw(Error);
不可能在 Chai 框架开始查看您的 .to.throw()
链之前处理抛出的异常。上面的代码 调用 函数 在 调用 expect()
之前,所以异常发生得太快了。
相反,您应该将函数传递给 expect()
:
expect(function() { subject.formatPostCodeStatus(status); })
.to.throw(Error);
这样,框架可以在为异常准备好后调用函数。