javascript Jasmine:如何监视内部对象方法调用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18483959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jasmine: how to spy on inner object method call?
提问by Agafonova Victoria
I have two prototypes I want to test:
我有两个要测试的原型:
var Person = function() {};
Person.prototype.pingChild = function(){
var boy = new Child();
boy.getAge();
}
var Child = function() {};
Child.prototype.getAge = function() {
return 42;
};
What exactly I want to test: to check that getAge()
method is called inside of the pingChild()
method
我到底想测试什么:检查该getAge()
方法在pingChild()
方法内部被调用
That is Jasmine specs I try to use for this purpose:
这是我尝试用于此目的的茉莉花规格:
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();
});
});
but all of them shows just errors:
但所有这些都只显示错误:
- getAge() method does not exist
- getAge() method does not exist
- Expected spy getAge to have been called
- getAge() 方法不存在
- getAge() 方法不存在
- 预期 spy getAge 已被调用
So, is there any way to test such cases using Jasmine, and if yes - how can it be done?
那么,有什么方法可以使用 Jasmine 测试此类案例,如果是 - 怎么做?
回答by Andreas K?berle
You have yo spy on the prototype of Child
object.
你已经窥探了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();
});
});
回答by margabit
I don't think it's possible because inner object is not accesible from outside the parent object. It's all about the scope of your objects.
我认为这是不可能的,因为内部对象不能从父对象外部访问。这完全与您的对象的范围有关。
You could either expose your Child
object in Person
object by doing this:
您可以通过执行以下操作Child
在Person
object 中公开您的对象:
var Person = function() {
this.boy = new Child();
};
Person.prototype.pingChild = function(){
this.boy.getAge();
}
And then:
接着:
describe("Person", function() {
it("calls the getAge() function", function() {
var fakePerson = new Person();
var chi = fakePerson.boy;
spyOn(chi, "getAge");
fakePerson.pingChild();
expect(chi.getAge).toHaveBeenCalled();
});
});
Or delegate the initialization of the Child
outside the Person
object:
或者委托Child
外部Person
对象的初始化:
var Person = function(child) {
this.boy = child;
};
Person.prototype.pingChild = function(){
this.boy.getAge();
}
And then:
接着:
describe("Person", function() {
it("calls the getAge() function", function() {
var chi = new Child();
var fakePerson = new Person(chi);
spyOn(chi, "getAge");
fakePerson.pingChild();
expect(chi.getAge).toHaveBeenCalled();
});
});