简体   繁体   English

如何在开玩笑中模拟“请求”节点模块进行测试

[英]How to mock a 'request' node module for testing in jest

I am new to writing test cases in jest and i wanted to test 'mark' function and want to mock 'request' node module.我是开玩笑地编写测试用例的新手,我想测试“标记”function 并想模拟“请求”节点模块。 let's say this file's name is app.js and test file will be app.test.js假设这个文件的名字是 app.js 而测试文件是 app.test.js

Can someone tell how to write its test case?有人可以告诉如何编写它的测试用例吗?

const request = require("request")

var test = {
    mark: function(data,cb){
        data.url = "localhost"
        request(data, function(err,response,body){
            if(!response){
                err.response = false
            }
            cb(err,body)
        })
    }

}

module.exports = test;

If I understand your question correctly, you are asking two questions:如果我正确理解你的问题,你会问两个问题:

  1. How to write the test and mock the request如何编写测试和模拟请求
  2. What test cases you should write你应该写什么测试用例

Regarding the mock, I recommend using nock which is an npm module for mocking.network requests.关于 mock,我建议使用nock ,它是一个用于 mocking.network 请求的 npm 模块。

To the second question, I believe that you should map the logic in the function and create the test cases from there.对于第二个问题,我认为您应该 map function 中的逻辑并从那里创建测试用例。 Think about the function, look at every if else/calculation/loop and it's possible outcomes and create test cases from there.考虑 function,查看每个 if else/calculation/loop 及其可能的结果并从那里创建测试用例。

The mark function doesn't have a lot of logic, it's send a request, updates the err variable according to the response and calling the callback. mark function 没有太多逻辑,它发送请求,根据响应更新err变量并调用回调。

The only outcome we can test to to see that if the request works, the cb is called correctly without modifications.我们可以测试的唯一结果是,如果请求有效,cb 会在没有修改的情况下被正确调用。 And if the request returns empty, change the err var and call the callback.如果请求返回空,则更改 err var 并调用回调。

To do so we need to mock the request and return a response to match the test case and validate that the cb was called correctly (can be done with a mock).为此,我们需要模拟请求并返回匹配测试用例的响应并验证 cb 是否被正确调用(可以通过模拟来完成)。

Test case example:测试用例示例:

The test case may be a bit abstract since I don't have the real use case of the function but it shows the point测试用例可能有点抽象,因为我没有 function 的真实用例,但它说明了重点

it("Should mark response false when it does not exist", () => {
    const DATA = {} // your data object
    const callback = jest.fn();

    nock('localhost')
        .get('/example')
        .reply(500) // Exaple of mocking the resonse. Custom it to be more specific depending on mark.
        
    test.mark(DATA, callback);


    // Verify that the function was called.
    expect(callback).toHaveBeenCalled();
    // Verify that `err.response` was false.
    expect(callback.mock.calls[0][0].response).toBe(false)
})

You can read more about mocking and verifying parameters here .您可以在此处阅读有关 mocking 和验证参数的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM