Rspec:应该是(这个或那个) [英] Rspec: Should be (this or that)
问题描述
在两种(或更多)结果中的任何一个都可以接受的情况下,编写 rspec 的最佳方法是什么?
What would be the best way to write the rspec in a situation where either of two (or more) outcomes are acceptable?
这是我想要做的一个例子.这显然是错误的(我认为),但它应该为您提供我想要完成的工作的要点:
Here's an example of what I want to do. This is obviously wrong (I think), but it should give you the gist of what I'm trying to accomplish:
it "should be heads or tails" do
h="heads"
t="tails"
flip_coin.should be(h || t)
end
是的,我知道我可以编写自己的 rspec 匹配器should_be_one_or_the_other(option1,option2)",但这似乎有点多 - 我希望有更好的解决方案.
And yes, I'm aware I could write my own rspec matcher "should_be_one_or_the_other(option1,option2)", but that seems a bit much - I was hoping for a better solution.
推荐答案
ActiveSupport 提供 Object#in?
方法.您可以将其与 RSpec 结合使用,只需使用以下内容:
ActiveSupport provides Object#in?
method. You can combine it with RSpec and simply use the following:
flip_coin.should be_in(["heads", "tails"])
或者使用新的 Rspec 3 语法:
Or with new Rspec 3 syntax:
expect(flip_coin).to be_in(["heads", "tails"])
这篇关于Rspec:应该是(这个或那个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!