【问题标题】:Mocking with Nock, DELETE用 Nock 模拟,删除
【发布时间】:2017-07-24 08:33:34
【问题描述】:

如何用 nock 模拟删除

这是我到目前为止所做的:

var nock = require('nock');
var request = require('supertest')("http://localhost:8080");
var expect = require('chai').expect;
describe('Mocked tests for server components', function(){

var mockRequest = nock('http://localhost:8080');
it('Should Delete /user/removeuserskills', function(req, res){
    mockRequest
    .delete('/user/removeuserskills',{
      'email':'Johny@gmail.com',
      'user_skill':'accoutant'
    })
    .reply(200,{
     'status':200,
     'message': '200: Successfully deleted skill'
      });
    request
    .delete('/user/removeuserskills')
    .end(function(err, res){
      expect(res.body.status).to.equal(200);
    });

  });
});

我无法演示如何首先添加具有该技能的用户,然后将其删除。

现在使用这段代码,我得到一个未定义的主体。

编辑:

其实我已经得到了

{ Error: Nock: No match for request:

 { method:"DELETE", 
   url: "http://localhost:8080/user/removeuserskills"
 }
}

【问题讨论】:

  • 我没有收到回复。

标签: node.js mocha.js chai supertest nock


【解决方案1】:

我的做法是使用他们的.intercept API。这意味着您的代码应如下所示:

   mockRequest
    .intercept('/user/removeuserskills', 'DELETE', {
      'email':'Johny@gmail.com',
      'user_skill':'accoutant'
    })
    .reply(200,{
     'status':200,
     'message': '200: Successfully deleted skill'
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-27
    • 2021-11-05
    • 1970-01-01
    • 2021-05-31
    • 2020-09-02
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多