最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jasmine spyOn - method does not exist in jQuery anonymous function - Stack Overflow

programmeradmin8浏览0评论

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)?

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)?

Share Improve this question asked Jul 15, 2014 at 1:47 BenBen 5,43210 gold badges44 silver badges60 bronze badges 2
  • foo is defined inside another anonymous function. Why would it be accessible via window.foo? – Yury Tarabanko Commented Jul 15, 2014 at 15:24
  • I was trying all the solutions provided by other similar questions on the web. Feel free to point me in the direction of the correct answer. Honestly, I don't know what the containing object for an anonymous jQuery function is, so I don't know what object to use. – Ben Commented Jul 15, 2014 at 16:51
Add a ment  | 

3 Answers 3

Reset to default 3

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.

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');    
        })

    });
});

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);
    });
});
发布评论

评论列表(0)

  1. 暂无评论