Rspec:应该是(这个或那个)
在两个(或更多)结果之一可接受的情况下,编写 rspec 的最佳方式是什么?
这是我想做的一个例子。这显然是错误的(我认为),但它应该给你我想要完成的要点:
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)”,但是似乎有点多 - 我希望有更好的解决方案。
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
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
ActiveSupport 提供了
Object#in?
方法。您可以将它与 RSpec 结合起来,只需使用以下内容:或者使用新的 Rspec 3 语法:
ActiveSupport provides
Object#in?
method. You can combine it with RSpec and simply use the following:Or with new Rspec 3 syntax:
我知道这已经很旧了,但我在 RSpec 3.4 上遇到了这个,有一个
或
方法现在。所以这是有效的:I know this is old but in I ran into this on RSpec 3.4, there is an
or
method now. So this is valid:我可能会写这样的东西:
I'd probably write something like this:
另一种将期望放在“应该”右侧的写法:
Another way of writing it with the expectation on the right of the should:
如果应用
or
和be
匹配器if applied
or
withbe
matcher您可以通过
翻转
比较来解决此问题:expect(['head','tails']).to include(flip_coin)
You can solve this by
flipping
the comparison:expect(['head','tails']).to include(flip_coin)