否有可能使用Jasmine的toHaveBeenCalledWi

否有可能使用Jasmine的toHaveBeenCalledWi

本文介绍了是否有可能使用Jasmine的toHaveBeenCalledWith matcher和正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了Jasmine的的文档matcher是为了理解是否可以为参数传递正则表达式,如果该参数应该是一个字符串。不幸的是,这是不受支持的功能。还有一个请求此功能。

I have reviewed Jasmine's documentation of the toHaveBeenCalledWith matcher in order to understand whether it's possible to pass in a regular expression for an argument, if that argument is expected to be a string. Unfortunately, this is unsupported functionality. There's also an issue open on github requesting this functionality.

我在代码库中挖了一下,我看到如何在。我认为将它作为一个单独的匹配器实现更合适,这样就可以单独捕获抽象。

I've dug a bit into the codebase, and I see how it might be possible to implement this inside the existing matcher. I think it would be more appropriate to implement it as a separate matcher though, so that the abstraction is captured individually.

在此期间,什么可能是一个好的解决方法?

In the meantime, what might be a good workaround?

推荐答案

经过一番挖掘,我发现对象有一个调用属性,后者又有一个函数。此函数还有一个子属性 args ,它返回一个调用参数数组。

After doing some digging, I've discovered that Jasmine spy objects have a calls property, which in turn has a mostRecent() function. This function also has a child property args, which returns an array of call arguments.

因此,可以使用当需要检查字符串参数是否与特定正则表达式匹配时,以下序列对调用参数执行正则表达式匹配:

Thus, one may use the following sequence to perform a regexp match on call arguments, when one wants to check that the string arguments match a specific regular expression:

var mySpy = jasmine.createSpy('foo');
mySpy("bar", "baz");
expect(mySpy.calls.mostRecent().args[0]).toMatch(/bar/);
expect(mySpy.calls.mostRecent().args[1]).toMatch(/baz/);

非常简单。

这篇关于是否有可能使用Jasmine的toHaveBeenCalledWith matcher和正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 10:56