Aga*_*ria 4 javascript testing bdd unit-testing jasmine
我有两个我要测试的原型:
var Person = function() {};
Person.prototype.pingChild = function(){
var boy = new Child();
boy.getAge();
}
var Child = function() {};
Child.prototype.getAge = function() {
return 42;
};
Run Code Online (Sandbox Code Playgroud)
我究竟要测试的是:检查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();
});
});
Run Code Online (Sandbox Code Playgroud)
但所有这些都只显示错误:
- getAge()方法不存在
- getAge()方法不存在
- 已经调用了预期的间谍getAge
那么,有没有办法用Jasmine测试这样的情况,如果是的话 - 怎么办呢?
你对Child对象的原型有间谍.
describe("Person", function () {
it("calls the getAge() function", function () {
var spy = spyOn(Child.prototype, "getAge");
var fakePerson = new Person();
fakePerson.pingChild();
expect(spy).toHaveBeenCalled();
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9690 次 |
| 最近记录: |