node.js - 用 Nock 进行模拟,删除

标签 node.js mocha.js chai supertest nock

我如何用 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"
 }
}

最佳答案

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

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

关于node.js - 用 Nock 进行模拟,删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42592315/

相关文章:

node.js - 在使用nodejs时,如何读取从arduino获取的特定缓冲区数据?

javascript - setTimeout 在 Node.JS 中是如何工作的?

node.js - 如何在 prisma 中设置不同的数据库进行测试?

node.js - Mocha-phantomjs 测试无输出

javascript - 如何使用 Postman 验证响应正文中可能包含空值或字符串的节点?

javascript - 如何测试构造函数中抛出的错误?

node.js - 亚硝酸盐上的 SSL/HTTPS

javascript - 选择带有纯 js 与 C++ 附加实现的 Node.js 库

javascript - javascript 中的软断言

javascript - 如何在 Mocha 中查找嵌套属性/值对