正弦函数withWith(new Error())并带有确切的消息 [英] sinon calledWith(new Error()) and with exact message

查看:120
本文介绍了正弦函数withWith(new Error())并带有确切的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要测试此功能:

//user.js
function getUser(req,res,next){
helper.get_user(param1, param2, (err,file)=>{
        if (err)
            return next(err);}

我的测试功能:

it ("failed - helper.get_user throws error",sinon.test(function () {
    var req,res;
    var get_user = this.stub(helper,"get_user")
    get_user.yields(new Error("message"));
    var next = sinon.spy(next);
    user.get_user(req,res,next);
    expect(next).to.have.been.calledWith(new Error("other message"));

}))

对于我的断言,我使用的是sinon-chai语法. 这个测试通过了,尽管我希望它会失败. 因为我的代码不会引发错误消息.

for my assertion I'm using sinon-chai syntax. this test is passing even though i would expect it to fail. because my code doesn't throw message with the error.

我如何测试正确的消息引发了错误?

how can i test that an error is thrown with correct message?

推荐答案

我通常要做的是:

const next = stub();
someMiddleware(req, res, next);
expect(next).to.have.been.called();
const errArg = next.firstCall.args[0];
expect(errArg).to.be.instanceof(Error);
expect(errArg.message).to.equal("Your message");

请注意,我正在使用 dirty-chai 来保持友好.

Note that I am using dirty-chai to be eslint friendly.

HTH,

这篇关于正弦函数withWith(new Error())并带有确切的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆