0

I am trying to write a test that checks if a Constructor function used the .call() function on another function. Here is an example:

Code To Test:

function Mammal(name, color) {
  this.name = name;
  this.color = color;
};
Mammal.prototype.beep = function() {
  return "Arf, Arf"
};
Mammal.prototype.changeColor = function() {
  this.color = "brown"
};

// TEST IF MAMMAL.CALL WAS USED IN THE DOG CONSTRUCTOR
function Dog(name, color){
  Mammal.call(this,name, color);
} 

Current Test (Doesn't Work Properly:

describe("Dog Class", function(){

  beforeEach(function(){
    dog = new Dog("Stewie", "Red");
  });

  it("should have a Name and Mammal color in its constructor", function(){      
    expect(dog.color).toEqual("Red");
    expect(truck.name).toEqual("Stewie");
  });

  it("should be called with the Mammal Constructor", function(){
    var test = spyOn(window, "Mammal").andCallThrough();
    expect(test.wasCalled).toEqual(true);
  });
});

I have referred to this post and similar posts, but many of them provide examples setting up tests with method calls on Object Literals. ".call" would be a Function for Function objects.

My current test doesn't change the wasCalled property to "true" even though Mammal.call() is being invoked in the Dog constructor function. How can I set up my test to check that Mammal.call() was used in the Dog Constructor?

Community
  • 1
  • 1
HelloWorld
  • 10,529
  • 10
  • 31
  • 50
  • where do you want to tell that from? inside of Dog(), it's pretty obvious by looking at the code... – dandavis Jan 13 '15 at 22:08
  • Don't test it's calling the mammal constructor instead make sure it's exposing the right members - also make sure to set the prototype otherwise you won't get any methods. As for testing you can override the .call method on mammal if you really need to. – Benjamin Gruenbaum Jan 13 '15 at 22:10
  • @BenjaminGruenbaum: would a modified Mammal.call catch stuff like Function.call.call(Mammal, this) ?, or would inspecting _this_ inside of Mammal() be more reliable? – dandavis Jan 13 '15 at 22:13
  • @dandavis no, it would not - but then again I don't believe in this use case to begin with. – Benjamin Gruenbaum Jan 13 '15 at 22:14

0 Answers0