【发布时间】:2013-08-31 07:03:19
【问题描述】:
我有两个要测试的原型:
var Person = function() {};
Person.prototype.pingChild = function(){
var boy = new Child();
boy.getAge();
}
var Child = function() {};
Child.prototype.getAge = function() {
return 42;
};
我到底想测试什么:检查getAge() 方法是否在pingChild() 方法内部调用
这是我尝试为此目的使用的 Jasmine 规格:
describe("Person", function() {
it("calls the getAge() function", function() {
var fakePerson = new Person();
var chi = new Child();
spyOn(fakePerson, "getAge");
fakePerson.pingChild();
expect(chi.getAge).toHaveBeenCalled();
});
});
describe("Person", function() {
it("calls the getAge() function", function() {
var fakePerson = new Person();
spyOn(fakePerson, "getAge");
fakePerson.pingChild();
expect(fakePerson.getAge).toHaveBeenCalled();
});
});
describe("Person", function() {
it("calls the getAge() function", function() {
var fakePerson = new Person();
var chi = new Child();
spyOn(chi, "getAge");
fakePerson.pingChild();
expect(chi.getAge).toHaveBeenCalled();
});
});
但所有这些都只显示错误:
- getAge() 方法不存在
- getAge() 方法不存在
- 预期的间谍 getAge 已被调用
那么,有没有什么方法可以使用 Jasmine 测试这种情况,如果可以,怎么做?
【问题讨论】:
标签: javascript unit-testing testing bdd jasmine