摩卡文字中的对象"undefined"。不确定如何传递价值



我正在一堆端点上运行摩卡测试,但我无法将结果传递给下一个测试用例第 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);
    }
});```

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

尝试像这样修改代码:

    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

对于您的情况,您没有定义,因为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"];
  ....
});

希望对你有帮助

最新更新