Rspec - expecting one among specific outputs

44 views Asked by At

Working on rspec(2.14.1), rspec-core (2.14.8) & rspec-expectations(2.14.5) versions

Need to expect one of two specific outputs. Tried below statements and got respective errors,

expect(fruit).should be_in('Apple','Mango')

Error

NoMethodError: undefined method `should' for #<RSpec::Expectations::ExpectationTarget:0x007fa63c4336d8>

and

expect(fruit).to eq('Apple').or(eq('Mango'))

and

expect(fruit).to include('Apple').or(eq('Mango'))

Error

NoMethodError: undefined method `or' for #<RSpec::Matchers::BuiltIn::Eq:0x007fa63c431630>

Searched a lot but couldn't find a solution. Is there a way to do it without having to update rspec to 3?

2

There are 2 answers

0
WeezHard On

Try this:

expect(fruit).to be_in(['Apple','Mango'])

You are expecting an element (fruit) to be in an array (['Apple', 'Mango'])

0
SteveTurczyn On

You can do...

expect(['Apple', 'Mango'].include?(fruit)).to be true