Today I learned how to write compound conditions in RSpec when writing tests for a Rails backend. #200wordsTIL
Say you have a method that returns an array. How do you check that it contains value1 OR value2? 🤔
There's the `includes` matcher. That lets you check that an array includes a value. You use it like this:
my_result is your method result, value1 is the value you're checking for.
So if my_result is something like [1,2,3,4] your test passes, if value1 is any of 1, 2, 3, or 4. Otherwise your test fails. 👍
Cool part of RSpec is how readable your tests come out.
Want to check for multiple values? Easy.
Now your test passes iff my_result includes both values.
You can also write expect(my_result).to include(value1, value2) to save space.
Ok so writing an AND is easy. But how do you make sure the result includes value1 OR value2?
There's a .or operator! 😱
expect(my_result).to include(value1).or include(value2)
You can chain as many .or as you want and you can use different matchers too. Very nice ✌️