0

Hi i have a works tests

it("should add a model", function() {

    spyOn($.fn, "val").and.returnValue("Bar");

    foodtype.addNewFoodtype(); //My view 
    expect($("#newFoodtype").val()).toEqual("Bar");
    expect(foodtype.collection.length).toEqual(1);

});

on next test when I set

spyOn($.fn, "val").and.returnValue("Bar");
$("#newFoodtype").val()
spyOn($.fn, "val").and.returnValue("Foo");
$("#newFoodtype").val()

to checking a change but have a error

Error: spyOn : val has already been spied upon Usage: spyOn(object, methodName)

Przemek eS
  • 1,224
  • 1
  • 8
  • 21

1 Answers1

1
  • You'll just need to return a new value instead of spying on it again.
  • Here is how I modified your code to make it work.
  • assigned a variable to spy so that I can access it once again spyObj.and.returnValue("Foo");
  • Please note that I've used a dummy view foodtype which mimics your view.

    var foodtype = {
      addNewFoodtype: function() {
        $("#newFoodtype").val("some val");
        this.collection.push("some val");
      },
      collection: []
    }
    describe("multispy demo", function() {
      it("should add a model", function() {
        var spyObj = spyOn($.fn, "val").and.returnValue("Bar");
        foodtype.addNewFoodtype(); //My view 
        expect($("#newFoodtype").val()).toEqual("Bar");
        expect(foodtype.collection.length).toEqual(1);
        spyObj.and.returnValue("Foo");
        expect($("#newFoodtype").val()).toEqual("Foo");
      });
    
    });
    
Winter Soldier
  • 2,607
  • 3
  • 14
  • 18