I have an rspec test where I want to ensure that only two values can be passed into my instance variable. I noticed that should satisfy
with a block seems to be the more accepted way to test where two outcomes are acceptable, but i'd like to implement an expect().to eq()
as illustrated below:
it "only allows -2 or 2 as values" do
expect (@note.value).to eq(2 or -2)
end
Is this correct syntax for my desired rspec testing? I understand the idea behind a should
statement, and i'm more concerned with the idea as to why should
is preferable (if that's actually the case) to using expect .to eq
with compound expectations.
Thank you.
You can or
two matchers together:
expect(@note.value).to eq(2).or eq(-2)
For more info see.
As mentioned in another question, you could do it the other way round and have the possible values in the expect
clause. With the new matcher syntax this would look like this:
expect([2, -2]).to include(@note.value)
I should also mention that you'll usually want to avoid this kind of randomness in your tests. Be precise about your expectations!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With