使用Chai断言库向Mocha提问

时间:2019-05-27 16:51:22

标签: javascript node.js mocha chai

我正在将main()从一个脚本导入到我的测试文件中,尽管main由多个函数组成,但我正在将main()作为1个测试用例执行。我想知道如何构造我的测试,使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
    }

测试

    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');

      });
    });

1 个答案:

答案 0 :(得分:0)

也许您可以使用.tags-list li{display:inline-block;background-color:#253b5d; padding: 1px 10px !important; text-transform: lowercase; font-weight: normal; font-size: 14px; transition: all 1s linear; } .tags-list li:hover{background-color: #aebdd5;} .tags-list li a{transition: all 1s linear;color:white;} .tags-list ul li a:hover{color:black;}并将每个期望值放入<ul class="tags-list"> <li><a href="#">Testing</a></li> <li><a href="#">Testing</a></li> <li><a href="#">Testing</a></li> <li><a href="#">Testing</a></li> <li><a href="#">Testing</a></li> <li><a href="#">Testing</a></li> </ul>函数中。

例如

before()

希望有帮助