【问题标题】:Adonis session not persisting between testsAdonis 会话在测试之间没有持续存在
【发布时间】:2021-05-14 21:38:31
【问题描述】:

我在 Adonis JS 上构建了一个简单的电子商务应用程序。我正在尝试测试购物车功能,如果用户将商品添加到购物车并且它已经在购物车中,那么它应该只是增加数量。为此,我两次点击购物车 API 来检查功能。但是,我的会话不会在请求之间持续存在,因此每次我只收到一个数量。

test('should increase the quantity if user tries to add the same item to cart', async ({ assert, client }) => {
    const menu = await createMenu(client);
    // Simply adds to cart
    await client.post(`/cart/${menu.id}`).send({
        quantity: 1,
    }).end();

    // It should increase the quantity to 2 now.
    const response = await client.post(`/cart/${menu.id}`).send({
        quantity: 1,
    }).end();

    response.assertStatus(200);
    // There should be only one item in cart
    assert.equal(response.body.data.items.length, 1);
    // And quantity of that item should be 2.
    assert.equal(response.body.data.items[0].quantity, 2);
});

【问题讨论】:

    标签: node.js session testing adonis.js


    【解决方案1】:

    方法是使用supertest

    const supertest = require('supertest');
    
    test('should increase the quantity if user tries to add the same item to cart', async ({ assert, client }) => {
    
        const BASE_URL = 'http://localhost:4000';
        const agent = supertest.agent(BASE_URL);
    
        const menu = await createMenu(client);
    
        // Simply adds to cart
        const result1 = await agent.post(`/cart/${menu.id}`).send({
                quantity: 1,
        }).withCredentials();
    
        assert.equal(result1.status, 200);
    
        // It should increase the quantity to 2 now.
        const result2 = await agent.post(`/cart/${menu.id}`).send({
                quantity: 1,
        }).withCredentials();
    
        assert.equal(result2.status, 200);
        // There should be only one item in cart
        assert.equal(result2.body.data.items.length, 1);
        // And quantity of that item should be 2.
        assert.equal(result2.body.data.items[0].quantity, 2);
    });
    

    【讨论】:

      猜你喜欢
      • 2019-04-19
      • 2013-11-08
      • 2015-02-27
      • 2021-10-29
      • 1970-01-01
      • 2017-12-28
      • 2011-10-10
      • 2022-01-19
      • 2020-09-05
      相关资源
      最近更新 更多