如何模拟a请求'在开玩笑中进行测试的节点模块

发布于 2025-01-23 03:51:09 字数 438 浏览 0 评论 0原文

我是在开玩笑中编写测试用例的新手,我想测试“标记”功能并想模拟“请求”节点模块。假设该文件的名称是app.js,并且测试文件将是app.test.js

有人可以告诉如何编写其测试用例?

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;

I am new to writing test cases in jest and i wanted to test 'mark' function and want to mock 'request' node module. let's say this file's name is app.js and test file will be 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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦忆晨望 2025-01-30 03:51:09

如果我正确理解您的问题,您会问两个问题:

  1. 如何编写测试并模拟请求
  2. 应该写的

有关模拟的测试案例,我建议使用 nock 这是用于模拟网络请求的NPM模块。

在第二个问题上,我相信您应该在功能中绘制逻辑并从那里创建测试用例。考虑一下功能,查看所有其他/计算/循环,这是可能的结果,并从那里创建测试用例。

标记函数没有很多逻辑,它是发送请求,根据响应来更新err变量并调用回调。

我们可以测试的唯一结果是,如果请求有效,则可以正确调用CB,而无需修改。如果请求返回空,请更改err var并调用回调。

为此,我们需要模拟请求并返回响应以匹配测试案例并验证CB是否正确调用(可以使用模拟完成)。

测试案例示例:

测试案例可能有点抽象,因为我没有该功能的真实用例,但它显示了点

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)
})

您可以阅读有关嘲笑和验证参数的更多信息,在这里

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.

To the second question, I believe that you should map the logic in the function and create the test cases from there. Think about the function, look at every if else/calculation/loop and it’s possible outcomes and create test cases from there.

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.

The only outcome we can test to to see that if the request works, the cb is called correctly without modifications. And if the request returns empty, change the err var and call the callback.

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).

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文