【问题标题】:mocking route path in react在反应中模拟路线路径
【发布时间】:2019-04-27 02:48:22
【问题描述】:

我想模拟路由路径,例如[http://localhost:3000/bookings/:bookingid] 用于在 app.test.js 文件中进行测试。如何实现?

【问题讨论】:

    标签: reactjs axios jestjs enzyme


    【解决方案1】:

    你可以通过测试路由函数的内容和mock自己的Request对象来测试路由。

    这是一个模拟测试路线的示例:

    app.js

    router.post("/bookings/:bookingid", (req, res) => {
    
        // validate credentials
        const error = validateCredentials(req.body.username, req.body.password, req.params.bookingid);
    
        ...
    }
    

    app.test.js

    describe("Booking routes", function() {
        const username = "John";
        const password = "stackoverflow";
        const bookingid = `123`;
    
        it("gets booking id", function() {
            expect(validateCredentials(username, password, bookingid)).to.be.undefined;
        });
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-14
      • 2021-11-30
      • 2022-01-17
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      • 2019-08-03
      相关资源
      最近更新 更多