代码之家  ›  专栏  ›  技术社区  ›  David Gustavsson

摩卡/柴-测试特定设置和拆卸

  •  2
  • David Gustavsson  · 技术社区  · 8 年前

    我知道摩卡有前后的全球测试,以及前后的每次测试,但我想要的是前后的具体测试。有点像SoapUI。

    例如,假设我有一个测试,检查用户的创建是否有效。

    1 回复  |  直到 8 年前
        1
  •  6
  •   Louis    8 年前

    对于需要特殊设置和拆卸代码,但在其他方面无法与其他测试区分开来的测试,我只放了一个 describe 标题为空的块:

    describe("SomeClass", () => {
        describe("#someMethod", () => {
            it("does something", () => {});
            it("does something else", () => {});
    
            describe("", () => {
                // The before and after hooks apply only to the tests in
                // this block.
                before(() => {});
                after(() => {});
    
                it("does something more", () => {});
            });
        });
    });
    

    可以 将设置和分解代码放入测试本身(即,在您传递给的回调中) it 任何 作为失败 同样的方法:测试失败。如果您希望Mocha以不同于测试失败的方式处理安装/拆卸代码中的失败,那么您可以 如我上面所示使用挂钩。