admin管理员组文章数量:1321809
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 viawindow.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
3 Answers
Reset to default 3I hate to answer my own question, but I rather that than other people not learning from my mistakes.
There are 2 issues here:
- Jasmine needs a containing object
- 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);
});
});
本文标签: javascriptJasmine spyOnmethod does not exist in jQuery anonymous functionStack Overflow
版权声明:本文标题:javascript - Jasmine spyOn - method does not exist in jQuery anonymous function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742106899a2421061.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论