Jasmine spyOn - method does not exist in jQuery anonymous function

梦想的初衷 提交于 2019-12-10 09:59:57

问题


Have searched for the answer to this problem on SO and other forums. Didn't find solution.

I'm running into a problem where Jasmine cannot find a function inside of a jQuery code block, whether the Jasmine code is inside the jQuery function or not.

$(function() {
    function foo(param1, param2) {
        // some code
    }

    describe("Foo function", function () {

        spyOn(window, 'foo');

        foo(1, 2);

        it("Should be found by Jasmine", function(){
            expect(window.foo).toHaveBeenCalled();
        });
    });
});

Error: foo() method does not exist

I have also tried spyOn($, 'foo'), spyOn($.fn, 'foo') and others.

Also, the following code does not work either.

$(function() {
    function foo(param1, param2) {
        // some code
    }
});

describe("Foo function", function () {

    spyOn(window, 'foo');

    foo(1, 2);

    it("Should be found by Jasmine", function(){
        expect(window.foo).toHaveBeenCalled();
    });
});

So how would one make both of these code blocks work (i.e. where the Jasmine code is inside of the jQuery function, and where it is outside of the jQuery function)?


回答1:


I hate to answer my own question, but I rather that than other people not learning from my mistakes.

There are 2 issues here:

  1. Jasmine needs a containing object
  2. You cannot access functions/variable outside the scope of a JS function

1 - Jasmine spies need to use a containing object, and if one can't be found, you just need to make it yourself. So the following code works:

// Using Jasmine inside of a jQuery anon function
$(function () {

    function foo() {
        // Some code
    }

    describe("Foo function", function () {

        it("Should be findable by Jasmine", function () {
            var Thing = {}
            Thing.foo = foo;
            spyOn(Thing, 'foo');
            Thing.foo(2)
            expect(Thing.foo).toHaveBeenCalled();
        });
    });
});

2 - If you have the describe block outside of the jQuery anon function, it won't be able to access any values from within the jQuery block, hence the second code example above won't work, no matter what you do.




回答2:


Not sure why would you need spies for "private" functions. You can do the following Demo.

$(function(){
    function foo() {
        return 'foo';
    };

    function baz() {
        return foo();    
    }

    describe('foo', function() { 
        //create a spy and define it to change foo
        foo = jasmine.createSpy().andCallFake(foo);


        it('should be a function', function() {
            expect(typeof foo).toBe('function');             
        });        

        var result = baz(); //call function you want to test

        it('should be called', function() {            
            expect(foo).toHaveBeenCalled(); //check if foo was called by baz   
        });        

        //additional check for return since .andCallThrough won't work
        it('should return foo', function() { 
            expect(result).toBe('foo');    
        })

    });
});



回答3:


It looks like you just need to set up your jQuery function differently. Try...

(function ($) {
    $.fn.foo = function (param1, param2) {
        // some code
    };
}(jQuery));

and then your Jasmine script will look like...

describe("Foo function", function () {

    var test = spyOn($.fn, 'foo');

    $.fn.foo(1,2);

    it("Should be found by Jasmine", function(){
        expect(test).toHaveBeenCalled();
        expect(test).toHaveBeenCalledWith(1,2);
    });
});


来源:https://stackoverflow.com/questions/24748702/jasmine-spyon-method-does-not-exist-in-jquery-anonymous-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!