Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec: using OR in expect .to eq statement [duplicate]

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.

like image 479
dpg5000 Avatar asked Aug 15 '15 05:08

dpg5000


2 Answers

You can or two matchers together:

expect(@note.value).to eq(2).or eq(-2)

For more info see.

like image 164
Myron Marston Avatar answered Nov 07 '22 09:11

Myron Marston


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!

like image 3
panmari Avatar answered Nov 07 '22 09:11

panmari