从灯具动态地将字符串传递给`it`?

时间:2019-06-06 14:21:15

标签: cypress

我想从固定装置(解析为it)动态地将字符串传递到permissionsObj。请参见代码中的A行。

dynamic string = some__static__string + ${headerPerms}

headerPerms = permissionsObj['header'];

但是以某种方式,当我将headerPerms传递给it

代码:

A行: headerPerms未定义。

B行: headerPerms成功记录。

无论如何,我可以从固定装置动态地将字符串传递给it吗?

describe(`role based testing:: header`, function () {
    let headerPerms;
    before(() => {
        cy.fixture('../fixtures/permissions').then((permissionsObj) => {
            headerPerms = permissionsObj['header'];
        });
        cy.wait(1000);
    });

    it(`checks if dashboard_KB exists:: ${headerPerms}`, function () { // line A: headerPerms is undefined
        cy.log(headerPerms);//Line B: logs successfully
    });
});

截屏:

enter image description here


装置:

{
  "header": {
    "dashboard_nav": {
      "admin": true,
      "developer": true,
      "analyst": true,
      "tester": true
    },
    "dashboard_KB": {
      "admin": true,
      "developer": true,
      "analyst": false,
      "tester": false
    },
    "dashboard_analytics": {
      "admin": true,
      "developer": true,
      "analyst": true,
      "tester": false
    },
    "dashboard_reports": {
      "admin": true,
      "developer": true,
      "analyst": false,
      "tester": false
    },
    "header-profile": {
      "admin": true,
      "developer": true,
      "analyst": true,
      "tester": true
    },
    "header-enterprise-profile": {
      "admin": true,
      "developer": false,
      "analyst": false,
      "tester": false
    },
    "header-sign-out": {
      "admin": true,
      "developer": true,
      "analyst": true,
      "tester": true
    }
  }
}

1 个答案:

答案 0 :(得分:0)

您不能像这样链接对象值。基本上,您会丢失上下文,因此会看到标头值为未定义。

使用下面的代码片段,就像魅力一样。

body

我已经验证并且可以正常工作。...下面是相同的输出。

const fixtures = require("../fixtures/headersJsonFile");
describe('feature', function () {

    const headers = Object.keys(fixtures.header);
    headers.forEach(header => {
        it(`header ${header} test`, () => {
            cy.log('', header);
        });
    });

});