【发布时间】:2015-02-18 22:37:45
【问题描述】:
所以我有这个代码:
describe('main describe', function() {
afterEach(function() {
//this.prop === undefined
});
describe('sub', function() {
it('should do something', function() {
this.prop = 'test';
});
});
});
我不知道为什么main 中的this.prop afterEach 是undefined,因为以下代码按预期工作:
describe('main describe', function() {
afterEach(function() {
//this.prop === 'test'
});
it('should do something', function() {
this.prop = 'test';
});
});
为什么第一个代码不能像我一样工作,尽管 this.prop 应该等于 'test' 而不是 undefined?
this 关键字是否仅与它直接包含的 describe 函数相关联?
【问题讨论】:
-
作为参考,这些是我能找到的文档:github.com/mochajs/mocha/wiki/Shared-Behaviours(尽管目前他们还没有回答这个具体问题)
标签: javascript unit-testing mocha.js