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 endSecond, 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.
更多推荐
发布评论