javascript Jasmine 测试一个对象是否有某个方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15676402/
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
Jasmine to test whether a object has a certain method or not
提问by Feel Physics
I am using Jasmine, and I want to test whether a object has a certain method or not, like below:
我正在使用 Jasmine,我想测试一个对象是否具有某种方法,如下所示:
it "should have 'open' method", ->
@xhr = ajax.create()
expect(@xhr).toHaveMethod "open" # I want to test like this!
How can I test? Thanks for your kindness.
我该如何测试?谢谢你的好心。
回答by Sukima
@david answered it correctly. toBeDefined()
is probably what you want. However if you want to verify that it is a function and not a property then you can use toEqual()
with jasmine.any(Function)
. Here is an example: http://jsbin.com/eREYACe/1/edit
@david 回答正确。toBeDefined()
可能是你想要的。但是,如果您想验证它是一个函数而不是一个属性,那么您可以使用toEqual()
with jasmine.any(Function)
。这是一个例子:http: //jsbin.com/eREYACe/1/edit
回答by HoLyVieR
There isn't a built-in way to do it, but you can achieve the desired result by doing this.
没有内置的方法可以做到这一点,但您可以通过这样做来获得所需的结果。
it "should have 'open' method", ->
@xhr = ajax.create()
expect(typeof @xhr.open).toBe("function")
Do note that testing if it's defined as proposed in the other answer has some edge cases where it will pass and it shouldn't. If you take the following structure, it will pass and it's definitely not a function.
请注意,测试它是否按照另一个答案中的建议定义有一些边缘情况,它会通过而它不应该通过。如果你采取以下结构,它会通过,它绝对不是一个函数。
{ open : 1 }
回答by David
回答by YourReflection
I did it like this. Angular example:
我是这样做的。角度示例:
beforeEach(inject(function ($injector) {
service = $injector.get("myService");
}));
it("should have myfunction function", function () {
expect(angular.isFunction(service.myfunction)).toBe(true);
});
回答by bas080
Jasmine allows you to write your own "matchers". The documentation explains it. http://jasmine.github.io/2.0/custom_matcher.html
Jasmine 允许您编写自己的“匹配器”。文档解释了它。 http://jasmine.github.io/2.0/custom_matcher.html
You could write a very specific matcher that is called
您可以编写一个非常具体的匹配器,称为
expect(obj).toHaveMethod("methodName");
Personally I would write something a bit more generic which checks the value type. That way I could use it to not just check if a method is defined on and object/instance, but anything that can store a value. It also gives me the possibility to check for types other then the function type.
我个人会写一些更通用的东西来检查值类型。这样我不仅可以使用它来检查是否在对象/实例上定义了方法,还可以使用它来检查任何可以存储值的东西。它还使我可以检查除函数类型之外的类型。
expect(obj.methodName).toBeA(Function);
To get it to work you do have to make sure to add the toBeA "matcher".
为了让它工作,你必须确保添加 toBeA“匹配器”。
beforeEach(function(){
jasmine.addMatchers({
toBeA: toBeA
});
});
function toBeA() {
return {
compare: function (value, type) {
var result = {pass: val != null && val.constructor === type || val instanceof type};
if (result.pass) {
result.message = 'Expected ' + value + ' to be a ' + type.name
} else {
result.message = 'Expected ' + value + ' to not be a ' + type.name
}
return result;
}
};
}
回答by user2086656
I tried this solution and it works :
我试过这个解决方案,它的工作原理:
spyOn(@xhr,"open")
If there is not function open, it would throw Error because it can't start spyig on it
如果没有打开的函数,它会抛出错误,因为它无法启动 spyig