Note that there are some explanatory texts on larger screens.

plurals
  1. POJasmine: how to spy on inner object method call?
    primarykey
    data
    text
    <p>I have two prototypes I want to test:</p> <pre><code>var Person = function() {}; Person.prototype.pingChild = function(){ var boy = new Child(); boy.getAge(); } var Child = function() {}; Child.prototype.getAge = function() { return 42; }; </code></pre> <p>What exactly I want to test: <strong>to check that <code>getAge()</code> method is called inside of the <code>pingChild()</code> method</strong></p> <p>That is Jasmine specs I try to use for this purpose:</p> <pre><code>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(); }); }); </code></pre> <p>but all of them shows just errors:</p> <blockquote> <ul> <li>getAge() method does not exist</li> <li>getAge() method does not exist</li> <li>Expected spy getAge to have been called</li> </ul> </blockquote> <p><em><strong>So, is there any way to test such cases using Jasmine, and if yes - how can it be done?</em></strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload