Jasmine2:获取当前的规格名称

bom*_*ba6 18 jasmine protractor

在Jasmine 1.3中,我们有了获取当前规范和套件名称的选项:

describe("name for describe", function () {
    it("name for it", function () {
        console.log(this.suite.getFullName()); // would print "name for describe"
        console.log(this.description); // would print "name for it"
    });
});
Run Code Online (Sandbox Code Playgroud)

这在Jasmine 2.x中不再有效.谁知道如何取这些?

谢谢.

小智 8

我添加了一个新的jasmine报告器,然后在每个规范上获取规范名称而不定义N变量.希望可以帮助,谢谢.

var reporterCurrentSpec = {
     specStarted: function(result) {
         this.name = result.fullName;
     }
 };
jasmine.getEnv().addReporter(reporterCurrentSpec);
Run Code Online (Sandbox Code Playgroud)


Ian*_*Ian 5

这不再起作用的原因是因为this不是测试.您可以对声明进行细微更改,但要修复它.而不仅仅是:

it("name for it", function() {});
Run Code Online (Sandbox Code Playgroud)

it变量定义为:

var spec = it("name for it", function() {
   console.log(spec.description); // prints "name for it"
});
Run Code Online (Sandbox Code Playgroud)

这不需要插件,可以使用标准的Jasmine.


war*_*n26 -2

这可能有点晚了,但您可以在规范之外获取套件名称。请尝试以下代码:

  describe("name for describe", function () {
    console.log(this.getFullName()); // would print "name for describe"
    it("name for it", function () {
      //Your test spec
    });
  });
Run Code Online (Sandbox Code Playgroud)