最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jasmine: how to spy on inner object method call? - Stack Overflow

programmeradmin5浏览0评论

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

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

So, is there any way to test such cases using Jasmine, and if yes - how can it be done?

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

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

So, is there any way to test such cases using Jasmine, and if yes - how can it be done?

Share Improve this question edited Sep 22, 2016 at 11:34 C.d. 9,9957 gold badges42 silver badges52 bronze badges asked Aug 28, 2013 at 9:21 Victoria AgafonovaVictoria Agafonova 2,17810 gold badges34 silver badges55 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

You have yo spy on the prototype of Child object.

describe("Person", function () {
  it("calls the getAge() function", function () {
    var spy = spyOn(Child.prototype, "getAge");
    var fakePerson = new Person();
    fakePerson.pingChild();
    expect(spy).toHaveBeenCalled();
  });
});

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:

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:

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();
    });
});
发布评论

评论列表(0)

  1. 暂无评论