是否可以将 Jasmine 的 toHaveBeenCalledWith 匹配器与正则表达式一起使用? [英] Is it possible to use Jasmine's toHaveBeenCalledWith matcher with a regular expression?

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

问题描述

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

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?

推荐答案

经过一番挖掘,我发现Jasmine spy 对象具有 calls 属性,而该属性又具有 mostRecent() 函数.这个函数还有一个子属性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 匹配器与正则表达式一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆