【发布时间】:2018-09-15 12:06:16
【问题描述】:
考虑一下我通过 Karma 运行的以下 Jasmine 代码:
class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
getName() { this.getFullName() }
getFullName() { this.firstName + this.lastName }
}
describe('Test Person', () => {
beforeEach(() => {
const programmer = new Person('John', 'Gray');
spyOn(programmer, 'getFullName');
programmer.getName();
});
it('getName should call getFullName', () => {
expect(programmer.getFullName).toHaveBeenCalled();
})
});
我想检查programmer.getName 确实调用了programmer.getFullName。我知道这可以通过检查getName 的返回值在此处进行测试,但我想明确检查是否调用了getFullName,因为这是我正在使用的实际代码中的场景。我已经实现了如上所示的代码,但它不起作用。我哪里错了?
我检查了this 的帖子,但它在这里不起作用。
【问题讨论】:
标签: javascript unit-testing jasmine karma-jasmine