简体   繁体   English

Mocha的问题,使用Chai断言库

[英]Question for Mocha, using Chai assertion library

I am importing main() from one script into my test file and I'm executing main() as 1 test case although main consists of multiple functions. 我将main()从一个脚本导入到我的测试文件中,并且我将main()作为1个测试用例执行,尽管main包含多个函数。 I was wondering how can I structure my test that each function on main would count as a separate test case instead of 1 as whole 我想知道如何构建我的测试,main上的每个函数都算作一个单独的测试用例,而不是整个1

    async function main(config = {}) {
      const url = validateUrl(config.url);
      const origin = validateOrigin(config.origin);
      const bundleId = validateBundleId(config.bundleId);
      const username = validateUsername(config.username);
      const password = validatePassword(config.password);

      let pipeline;
      let message;
      let worker;
      let passthroughWorkerId;

      pipeline = await createPipeline(origin, bundleId, username, password, url);
      if (pipeline.status != "running") {
        throw new Error("Pipeline not started");
      }

      message = await pipelineWait(username, password, url, pipeline);
      if (debug) {
        console.log(`Wait message: ${message}`);
      } 

      worker = await updatePassthroughWorker(username, password, url, pipeline, 0);
      // Do something with worker 0
      pipeline = await retreivePipeline(username, password, url, pipeline);
      if (pipeline.status != "waiting") {
        throw new Error("Pipeline didn't wait");
      }
      pipeline = await restartPipeline(username, password, url, pipeline);
      if (pipeline.status != "running") {
        throw new Error("Pipeline didn't restart");
      }

      message = await cancelPipeline(username, password, url, pipeline);
      if (debug) {
        console.log("Pipeline Status:");
        console.log(pipeline.status);
        console.log(`Cancel message: ${message}`);
      }

      worker = await updatePassthroughWorker(username, password, url, pipeline, 1);

      pipeline = await retreivePipeline(username, password, url, pipeline);
      if (pipeline.status != "cancelled") {
        throw new Error("Pipeline didn't cancel");
      }

      return pipeline
    }

Test 测试

    describe('Pipeline cancellation', () => {

      it('Should trigger pipeline, put it in wait, resume, and the cancel it ', async () => {
        let data = await pipeline.main(config[process.env.TEST_ENV]);
        expect(data).to.be.an('object');
        expect(data).to.have.property('status');
        expect(data.status).to.equal('cancelled');

      });
    });

Perhaps you can use before() and put each expectation in it() function. 也许你可以使用before()并将每个期望放在it()函数中。

eg 例如

describe('Pipeline cancellation', () => {
  let data;

  before(async () => {
    data = await pipeline.main(config[process.env.TEST_ENV]);
  });

  it('Should trigger pipeline', () => {
    expect(data).to.be.an('object');
    expect(data).to.have.property('status');
    expect(data.status).to.equal('cancelled');
  });

  it('should put it in wait', () => {
    // ...
  });

  it('should resume', () => {
    // ...
  });
});

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM