【问题标题】:sinon.match() dont match object with Json.stringify responsesinon.match() 不匹配对象与 Json.stringify 响应
【发布时间】:2019-06-18 14:28:31
【问题描述】:

我正在测试一个简单的快速路由,它返回一个 json 对象并使用 mocha 和 sinon 来匹配响应。

这是简单的快速响应 /login

            const login = async (req, res) => {
                let response = {
                    success: true,
                    id: "id0090",
                }
                res.end(JSON.stringify(response));
            }

这是 micha 和 sinon 的简单春节

it('res.end should be called with success true ', async () => {
    let req = {
        body: {}
    }
    let res = {
        end: sinon.spy(),
    }
    await login(req,res);
    let expected = {
        success: true,
        id: "id0090",
    }
    sinon.assert.calledWith(res.end, sinon.match(expected));

});

我收到此错误,但正如您所见,两者是相同的。

            AssertError: expected end to be called with arguments 
            {"success":true,"id":"id0090"} match(success: true, id: id0090)

有趣的是,如果我从 login 中删除 JSON.stringify,则测试通过,但这不是想法。

            const login = async (req, res) => {
                let response = {
                    success: true,
                    id: "id0090",
                }
                res.end(response); // this make the test pass
            }

感谢您的帮助

【问题讨论】:

    标签: javascript unit-testing mocha.js sinon


    【解决方案1】:

    因为 JSON.stringify 转换为字符串但预期结果是 JSON 对象所以失败了。

    【讨论】:

      猜你喜欢
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-10
      • 1970-01-01
      • 1970-01-01
      • 2016-01-12
      相关资源
      最近更新 更多