Javascript 无法让 jasmine.any(Function) 工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10610874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
can't get jasmine.any(Function) to work
提问by Mike S
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 ] ]
预期 spy 被调用为: [ [ 'hello',<jasmine.any(function Function() { [native code] })> ] ] 但被调用为: [ [ '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!
为什么不识别我传入的函数 (function (){}) 实际上是函数?它期望的本机代码是什么?其他人对 jasmine.any(Function) 有这个问题吗?谢谢!
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?
我尝试使用 spyOn 而不是 jasmine.createSpy() 并且它没有区别。我只尝试了一个参数,它起作用了。引入第一个字符串参数会破坏 jasmine.any(Function) - 有什么想法吗?
回答by Mike S
Ah, I thought you had to enclose the arguments of expect().toHaveBeenCalledWith
in an Array[]
. Silly me. Here is a working version:
啊,我以为您必须将 的参数括expect().toHaveBeenCalledWith
在Array[]
. 傻我。这是一个工作版本:
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));
});
});
回答by Andreas K?berle
The Problem is the way you create your spy, using spyOn
seems to work as expected:
问题是您创建间谍的方式,使用spyOn
似乎按预期工作:
describe("test", function() {
return it("is function", function() {
var a = {b: function(){}};
spyOn(a, 'b');
a.b(function(){});
expect(a.b).toHaveBeenCalledWith(jasmine.any(Function));
});
});
You could also write your own Matcherto test if the passed value is a function:
您还可以编写自己的 Matcher来测试传递的值是否为函数:
describe('test',function(){
it('is function',function(){
this.addMatchers({
isFunction: function() {
return typeof this.actual === 'function';
}
});
expect(function(){}).isFunction();
});
});
EDITED:codified the first code chunk
编辑:编码第一个代码块