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
I'd probably write something like this:
it "should be heads or tails" do
["heads", "tails"].should include flip_coin
end
ActiveSupport provides Object#in?
method. You can combine it with RSpec and simply use the following:
flip_coin.should be_in(["heads", "tails"])
Or with new Rspec 3 syntax:
expect(flip_coin).to be_in(["heads", "tails"])
if applied or
with be
matcher
expect(flip_coin).to eq('heads').or(be == 'tails')
You can solve this by flipping
the comparison:
expect(['head','tails']).to include(flip_coin)
I know this is old but in I ran into this on RSpec 3.4, there is an or keyword now. So this is valid:
expect(flip_coin).to eq('heads').or(eq('tails'))
Another way of writing it with the expectation on the right of the should:
it 'should be heads or tails' do
flip_coin.should satisfy{|s| ['heads', 'tails'].include?(s)}
end