javascript jasmine toHaveBeenCalledWith 部分匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21101038/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:04:06  来源:igfitidea点击:

jasmine toHaveBeenCalledWith partial matching

javascriptunit-testingjasmine

提问by Pwnna

With Jasmine, I could spy on methods and figure out the arguments. I want to be able to call toHaveBeenCalledWith(something, anything).

使用 Jasmine,我可以监视方法并找出参数。我希望能够打电话toHaveBeenCalledWith(something, anything)

Let's say I want to spy on a method .on(event, callback). All I care about is if the eventis listened to rather than what the actual callback identity is. Is it possible to do this without writing a custom matcher? I don't see one.

假设我想监视一个方法.on(event, callback)。我所关心的是是否event监听了 ,而不是实际的回调标识是什么。是否可以在不编写自定义匹配器的情况下执行此操作?我一个都没有。

回答by beautifulcoder

Try

尝试

toHaveBeenCalledWith(jasmine.any(Object), jasmine.any(Function))

回答by Cmag

If you wish to test for specific things, you can do something like:

如果您想测试特定的东西,您可以执行以下操作:

expect(mockSomething.someMethod.mostRecentCall.args[0].pool.maxSockets).toEqual(50);

The syntax in Jasmine 2 is now:

Jasmine 2 中的语法现在是:

mockSomething.someMethod.calls.mostRecent().args[0]

回答by Ievgen Martynov

Jasmine 2:

茉莉花2:

 expect(callback).toHaveBeenCalledWith(jasmine.objectContaining({
    bar: "baz"
  }));

https://jasmine.github.io/2.0/introduction.html

https://jasmine.github.io/2.0/introduction.html