Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
21
rated 0 times [  28] [ 7]  / answers: 1 / hits: 35732  / 12 Years ago, wed, may 16, 2012, 12:00:00

I've created a complete simplified example that replicates the problem I'm getting.



function TestObj() {
var self = this;
self.getStuff = function(aString, callback) {
// TODO
}
}

describe(server communications, function() {
it(it calls the server, function() {
var obj = new TestObj();
obj.getStuff = jasmine.createSpy();
// swap the above line for this and it makes no difference
// spyOn(obj, getStuff);

var functionVar = function() {
};

obj.getStuff(hello, functionVar);

expect(obj.getStuff).toHaveBeenCalledWith(
[ hello, jasmine.any(Function) ]);
});
});


Instead of a passing unit test, I get the following output:




Expected spy to have been called with:
[ [ 'hello',<jasmine.any(function Function() { [native code] })> ] ]
but was called with:
[ [ 'hello', Function ] ]




Why is it not recognising that the functions I pass in (function (){}) are actually functions? Whats that native code stuff it is expecting? Anyone else had this issue with jasmine.any(Function)? Thankyou!



EDITED



I tried using spyOn instead of jasmine.createSpy() and it makes no difference. I tried just a single argument and it works. Introducing the first string argument breaks the jasmine.any(Function) - any ideas?


More From » jasmine

 Answers
15

Ah, I thought you had to enclose the arguments of expect().toHaveBeenCalledWith in an Array[]. Silly me. Here is a working version:



function TestObj() {
var self = this;
self.getStuff = function(aString, callback) {
// TODO
}
}

describe(server communications, function() {
it(it calls the server, function() {
var obj = new TestObj();
obj.getStuff = jasmine.createSpy();
// swap the above line for this and it makes no difference
// spyOn(obj, getStuff);

var functionVar = function() {
};

obj.getStuff(hello, functionVar);

expect(obj.getStuff).toHaveBeenCalledWith(hello, jasmine.any(Function));
});
});

[#85558] Monday, May 14, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sidneyh

Total Points: 118
Total Questions: 108
Total Answers: 105

Location: Mali
Member since Fri, Jun 18, 2021
3 Years ago
sidneyh questions
Tue, Jun 7, 22, 00:00, 2 Years ago
Wed, Apr 13, 22, 00:00, 2 Years ago
Wed, Aug 12, 20, 00:00, 4 Years ago
Wed, Jun 3, 20, 00:00, 4 Years ago
Fri, Apr 24, 20, 00:00, 4 Years ago
;