在摩卡,
describe('this message text', function(){
it('and this message text', function(done){
console.log(this); // {} is empty
});
});
Run Code Online (Sandbox Code Playgroud)
如何'this message text' 'and this message text'从测试内部访问?
我试过this对象,但它是空的.
正如您所发现的那样,this在回调内部访问it不起作用.这是一种方法:
describe('this message text', function () {
var suite_name = this.title;
var test_name;
beforeEach(function () {
test_name = this.currentTest.title;
});
it('and this message text', function () {
console.log(suite_name, test_name);
});
it('and this other message text', function () {
console.log(suite_name, test_name);
});
});
Run Code Online (Sandbox Code Playgroud)
上面代码中的解决方法是beforeEach钩子在测试运行之前抓取测试名称并将其保存test_name.
如果您想知道this测试回调中的值是什么,它是ctx测试所属的套件上的字段的值.例如,console.log声明如下:
describe('suite', function () {
this.ctx.foo = 1;
it('test', function () {
console.log(this);
});
});
Run Code Online (Sandbox Code Playgroud)
输出:
{
"foo": 1
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1864 次 |
| 最近记录: |