Pactumjs - Cucumber:存储来自响应的令牌

问题描述 投票:0回答:1

我是 Pactumjs 的新手。 当我想存储我的不记名令牌以在另一个黄瓜步骤中重用它时,我在黄瓜步骤中遇到问题。

Given('I call the login service', async () => {
    await pactum.spec()
        .post('/api/rest/login')
        .withHeaders('Content-Type', 'application/json')
        .withBody({
            file: "json/login.json"
        })
        .expectStatus(200)
        .expect(ctx => {
            const token = ctx.res.body.token;
            chai.expect(token).is.not.null;
        });
});

When('I call the get points of sale service', async function () {
    await pactum.spec()
        .get('/api/rest/admin)
        .withHeaders({
            'Authorization': 'Bearer ',  // MY TOKEN
            'Content-Type': 'application/json'
        })
        .expectStatus(200);
});

我也试过使用“stores”命令但没有结果。

我怎么办?谁能帮帮我?

rest testing cucumberjs
1个回答
0
投票

理想情况下

stores
应该按预期工作。

const { string } = require('pactum-matchers');

Given('I call the login service', async () => {
    await pactum.spec()
        .post('/api/rest/login')
        .withHeaders('Content-Type', 'application/json')
        .withBody({
            file: "json/login.json"
        })
        .expectStatus(200)
        .expectJsonMatch({
          "token": string()
        })
        .stores('TOKEN', 'token');
});

When('I call the get points of sale service', async function () {
    await pactum.spec()
        .get('/api/rest/admin')
        .withHeaders({
            'Authorization': 'Bearer $S{TOKEN}',
            'Content-Type': 'application/json'
        })
        .expectStatus(200);
});
© www.soinside.com 2019 - 2024. All rights reserved.