简体   繁体   English

为什么即使未提供令牌,我的测试也会收到状态 200?

[英]why is my test receiving status 200 even though token is not provided?

I defined let token and logged in before each implements, but in this one i simply token = '' to make sure that client is not logged in. I am supposed to get status 401 but instead i am getting 200. Can you tell me what i am doing wrong?我在每个实现之前定义了let token并登录,但在这个我只是token = ''以确保客户端没有登录。我应该得到状态 401 但我得到了 200。你能告诉我什么我做错了吗?

describe('PUT /:id', () => {

        let token;
        let genre;
        let newName;
        let id;

        const exec = async () => {
            return await request(server)
                .put('/api/genres/' + id)
                .set('x-auth-token', token)
                .send({ name: newName });
        }

        beforeEach(async () => {
            genre = new Genre({ name: 'genre1' });
            await genre.save();

            token = new User().generateAuthToken();
            id = genre._id;
            newName = 'updatedName';
        });

        it('should return 401 if client is not logged in', async () => {
            token = '';

            const res = await exec();

            expect(res.status).toBe(401);
        });
 ● /api/genres › PUT /:id › should return 401 if client is not logged in

    expect(received).toBe(expected) // Object.is equality

    Expected: 401
    Received: 200

      142 |             const res = await exec();
      143 |
    > 144 |             expect(res.status).toBe(401);
          |                                ^
      145 |         });
      146 |
      147 |         it('should return 400 if genre is less than 5 characters', async () => {

      at Object.toBe (tests/integration/genres.test.js:144:32)

您可能需要向我们展示包含端点的路由文件夹以检查用户是否已登录,并尝试检查端点上是否需要您的 auth 中间件功能。

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

相关问题 jsonwebtoken,jwt 提供无效令牌时我的应用程序崩溃,即使 errorHandler 中间件捕获了错误 - jsonwebtoken, jwt crashing my app when invalid token is provided even though errorHandler middleware catches the error 为什么我的测试没有通过我的逻辑门,为什么仍能通过? - Why does my test pass, even though it doesn't meet my logic gate? 即使xmlhttp.status等于200,xmlhttp.readyState也会返回2 - xmlhttp.readyState returns 2 even though xmlhttp.status equals 200 即使响应http状态为200,也会执行jQuery ajax错误回调 - Jquery ajax error callback is executed even though the response http status is 200 收到200状态代码而不是201 - Receiving a 200 status code instead of a 201 为什么我的区间调用我的 function 即使它在一个变量中? - Why is my interval calling my function even though its in a variable? 即使状态为200,WNS推送通知也不起作用 - WNS Push notification not working even with status 200 即使状态为 200 也无法获取数据 - Not getting data in fetch even if the status is 200 in react 为什么我的表单即使返回false仍仍在提交? - Why is my form still submitting even though it returns false? 为什么我的JSON即使看起来正确也仍然无效? - Why is my JSON invalid even though it looks correct?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM