使用存根进行Javascript测试

时间:2016-10-18 20:15:05

标签: javascript sinon

我有这个功能

export const getBigData = (param) => {
  return new Promise((resolve) => {
    reqwestWrapper.post({
      url,
      data: {"someKey": "someData"},
      success: (result) => {
        resolve(result.data);
      }
    });
  });
}

我正在尝试编写一个测试断言.post正在调用正确的值,所以我测试为

let url = 'some-url',
    data = {},
    successCallback = sinon.stub();

expect(reqwest.post).calledWith({url, data, successCallback}) 

但测试失败说预期的论点不匹配。

AssertionError: expected post to have been called with arguments { data: {  }, successCallback: function stub() {}, url: "some-url" }
post({ data: {  }, success: function success() {}, url: "some-url" })

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您实际上并未在提供的代码中调用request.post。您只需设置呼叫测试即可。在测试之前,你需要做:

reqest.post({url, data, successCallback});