嘲笑superagent post请求与nock不匹配

时间:2015-12-31 00:18:47

标签: mocking mocha superagent nock

我不能为我的生活得到一个简单的superagent邮政请求工作。这是我的superagent和nock配置。

的SuperAgent:

request
  .post('https://test.com/api/login')
  .send({
    email: 'test@test.com',
    password: 'testpassword'
  })
  .end((err, res) => {
    if (err) {
      console.log(err);
    }
  });

诺克:

nock('https://test.com')
  .post('/api/login')
  .send({
    email: 'test@test.com',
    password: 'testpassword'
  })
  .reply(200, {
    id: 1,
    token: 'abc'
  });

我从nock收到以下错误:

  

{[错误:Nock:请求POST不匹配   https://test.com/api/login   {“email”:“test@test.com”,“password”:“testpassword”}]状态:404,   statusCode:404,响应:undefined}

另一个奇怪的方面是我的superagent响应处理程序中记录了此错误。所以我知道这个电话正在制作和截获。

2 个答案:

答案 0 :(得分:12)

好的 - 想通了。在深入研究文档之后,我找到了.log函数。我像这样链接了我的nock配置

nock('https://test.com')
    .log(console.log)...

并且事实证明请求正文不匹配。

答案 1 :(得分:0)

.send()被替换为在网址后发送帖子正文作为第二个参数,而.log.reply

之前不起作用
相关问题