我对茉莉花实际上如何处理全局环境有疑问。在api docs中,诸如beforeEach(),afterEach(), etc
之类的方法被描述为全局方法。因此,Jasmine可以识别它们而无需.
运算符来调用它们。
我有以下简单的课程:
export class VoteComponent {
totalVotes = 0;
upVote() {
this.totalVotes++;
}
downVote() {
this.totalVotes--;
}
}
并测试以下规格,效果很好:
import { VoteComponent } from './vote.component';
describe('VoteComponent', () => {
let Votes :VoteComponent;
beforeEach(()=>{
Votes = new VoteComponent();
})
it('Upvotes', () => {
Votes.upVote();
expect(Votes.totalVotes).toBe(1);
});
it('Downvotes', () => {
Votes.downVote();
expect(Votes.totalVotes).toBe(-1);
});
});
但是,如果我将规格更改为以下形式:
// let Votes :VoteComponent;
beforeEach(()=>{
let Votes = new VoteComponent();
})
我收到以下错误:
Chrome 67.0.3396(Windows 10.0.0)VoteComponent投票失败 ReferenceError:投票未定义
我不明白为什么会这样。 beforeEach()是全局上下文函数,在每个规范之前被调用。但即使如此,除非在beforeEach()外部实例化,否则在规范Votes
中仍无法识别。
这是为什么?我正在使用Angular-6,Karma 1.7和Jasmine 2.9