Mocha脚本中的对象“未定义”。不确定如何传递价值

时间:2019-05-30 17:33:29

标签: javascript mocha chai

我正在一系列端点上运行Mocha测试,但无法将结果传递给下一个测试用例 第var result = data["results"];行失败 error:data undefined


    it('should get series', async () => {
        let data = await legacy.getCategories(config[process.env.TEST_ENV].url, config[process.env.TEST_ENV].origin);
        console.log(data);
        expect(data).to.be.an('array').that.is.not.empty;
        done();
    });

    var unallowedGuids = config[process.env.TEST_ENV].unallowedGuids;
    var result = data["results"];

    for (i = 0; i < result.length; i++) { 
        let guid = result[i]["guid"];
        let showName = result[i]["title"];
        if (!unallowedGuids.includes(guid)){
                it('should get colletions guid', async () => {
                let data = await legacy.collectionsGuid(config[process.env.TEST_ENV].url, config[process.env.TEST_ENV].origin, guid);
                expect(data).to.be.an('array').that.is.not.empty;
                done();
    });
                it('should get series guid', async () => {
                let data = await legacy.seriesGuid(config[process.env.TEST_ENV].url, config[process.env.TEST_ENV].origin, guid);
                expect(data).to.be.an('array').that.is.not.empty;
                done();
    });
        } 
        else
            console.log("excluded", guid);
    }

});```

2 个答案:

答案 0 :(得分:0)

我认为您的问题与可变范围有关。

尝试像这样修改代码:

    let data; // initialize "data" outside of the "it" callback function

    it('should get series', async () => {
      data = await legacy.getCategories(config[process.env.TEST_ENV].url, config[process.env.TEST_ENV].origin);
      console.log(data);
      expect(data).to.be.an('array').that.is.not.empty;
      done();
    });

    var unallowedGuids = config[process.env.TEST_ENV].unallowedGuids;
    var result = data["results"]; // data will be defined here

答案 1 :(得分:0)

对于您而言,您不确定,因为var result = data["results"];在执行it('should get series', ..)函数之前先执行。

一种解决方案是重组测试以确保执行顺序正确。我们可以使用before()beforeEach()例如:

let data;

before(async () => {
  data = await legacy.getCategories(config[process.env.TEST_ENV].url, config[process.env.TEST_ENV].origin);                
});

it('get collection and series guid', async function() {
  var unallowedGuids = config[process.env.TEST_ENV].unallowedGuids;
  var result = data["results"]; // it will be executed after data is fetched in `before()`

  for (i = 0; i < result.length; i++) { 
      let guid = result[i]["guid"];
      let showName = result[i]["title"];
      if (!unallowedGuids.includes(guid)){
        let data = await legacy.collectionsGuid(config[process.env.TEST_ENV].url, config[process.env.TEST_ENV].origin, guid);
        expect(data).to.be.an('array').that.is.not.empty;

        let data = await legacy.seriesGuid(config[process.env.TEST_ENV].url, config[process.env.TEST_ENV].origin, guid);
        expect(data).to.be.an('array').that.is.not.empty;
      } 
      else {
        console.log("excluded", guid);
      }
  }
});

或者,您也可以将获取数据的功能放在相同的it()函数中

// no before()

it('get collection and series guid', async function() {
  var unallowedGuids = config[process.env.TEST_ENV].unallowedGuids;

  var data = await legacy.getCategories(config[process.env.TEST_ENV].url, config[process.env.TEST_ENV].origin);
  var result = data["results"];

  ....
});

希望有帮助