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(
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.