RSpec是否有内置匹配器来查看String是一个值还是另一个值?(Does RSpec have a built in matcher for seeing if a String is one v

编程入门 行业动态 更新时间:2024-10-19 11:57:19
RSpec是否有内置匹配器来查看String是一个值还是另一个值?(Does RSpec have a built in matcher for seeing if a String is one value or another?)

编辑:提供复合匹配器答案是一个更优雅的解决方案:

expect(@weathers.get_day_today).to eq('Today').or eq('Tonight')

原始问题:

我正在测试一个值是“今天”还是“今晚”。 有内置的匹配器吗? 还是比我正在做的更清洁的方法?

编辑:以下是我尝试进行自定义测试,但它不起作用。 这是一个不做的例子。

首先,添加自定义匹配器。

RSpec::Matchers.define :be_this_or_that do |expected| match do |actual| actual == expected.first or expected.last end end

其次,使用它

expect(@weathers.get_day_today).to be_this_or_that(["Today", "Tonight"])

Edit: Compound matcher answer provided is a much more elegant solution:

expect(@weathers.get_day_today).to eq('Today').or eq('Tonight')

Original Question:

I am testing if a value is either "Today" or "Tonight". Is there a built-in matcher? Or a cleaner method than what I'm doing?

Edit: The following was my attempt to do a custom test, but it doesn't work. Here's an example of what NOT to do.

First, add custom-matcher.

RSpec::Matchers.define :be_this_or_that do |expected| match do |actual| actual == expected.first or expected.last end end

Second, use it

expect(@weathers.get_day_today).to be_this_or_that(["Today", "Tonight"])

最满意答案

你可以尝试这样:

expect(@weathers.get_day_today).to eq('Today').or eq('Tonight')

如需更多复合期望,请访问此处 。

You can try like this:

expect(@weathers.get_day_today).to eq('Today').or eq('Tonight')

for more compound expectations you can visit here.

更多推荐

本文发布于:2023-08-04 22:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1424547.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是一个   String   RSpec   built   matcher

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!