使用 Node.js 和 Cucumber 在 REST API 上进行 BDD

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

大家好,我正在学习 BDD,我想开始使用它来测试我的 API,

这是黄瓜文件

  • 场景概要:通过id获取用户

  • 给定有效的用户 ID

  • 当我发送 GET 请求到 http://localhost:3000/api/gettingUser/id

  • 然后我得到响应状态代码应该是200

  • 用户响应正文应包含用户详细信息

    Examples: | request | {"id":2, "nombre":"roland",    "apellidoPaterno":"Rios", "apellidoMaterno":"Campos", "edad":26,    "email":"[email protected]"}
    

这是 api_steps.js

Given('A valid user Id', function () {
    return userID;
  });

  When('I send GET request to http://localhost:3000/api/gettingUser/1', function (int) {
      // Write code here that turns the phrase above into concrete actions
      return 'pending';
    });

    Then('I get response status code should be {int}', function (int) {
          return 'pending';
})

Then('the user response body should contain user details', function () {
    return 'pending';
  });

因为我对此很陌生,所以我做了研究,这就是我想出的结果。

有人可以指导我吗?

我真的很感激。

node.js bdd cucumberjs
1个回答
0
投票

我终于成功了!!!这就是我通过 3 个测试 const 的方式

{ Given, When, Then } = require('cucumber')
const axios = require('axios')
const { expect } = require('chai')

let userid = 2;
let response;

Given("a valid <userid>", async function () {
     return userid;
});

When("I send GET request to {string}", async function (arg1, docString) {
    const url = `http://localhost:3000/api/gettingUser/${userid}`
    response = await axios.get(url)

});

Then("the response body should contain user details", async function (docString) {
    console.log(docstring)
});
© www.soinside.com 2019 - 2024. All rights reserved.