Spying on JQuery Selectors in Jasmine

前端 未结 6 1446
执念已碎
执念已碎 2020-12-02 15:29

I am unit testing some JavaScript with Jasmine and wish to spy on (mock) an element of the DOM that is accessed by a jQuery selector.

My spec is:

it(         


        
6条回答
  •  不思量自难忘°
    2020-12-02 15:55

    The problem is that the two calls to $ return two different jQuery-wrapped nodes.

    This should work:

    it("should be able to mock DOM call", function(){
    
      // var node = $("Something");
      // spyOn(node, 'val').andReturn('bar');
    
      // expect(node.val()).toEqual('bar');
      var node = $("Something");
      spyOn(node, 'val').and.returnValue('bar');
    
      expect(node.val()).toEqual('bar');
    });
    

    Next time, help is more prevalent on the Jasmine mailing list: jasmine-js@googlegroups.com.

提交回复
热议问题