I'm using RSpec to test an API. I want to specify that one particular endpoint returns a boolean value, either true or false. This is an API to be used by other systems and languages, and so it's important for it to be an actual boolean, not merely something that Ruby will evaluate to false or true.
But I don't see a way to do this because there's no Boolean type in Ruby.
expect(variable).to be_in([true, false])
I had the same problem. This is what I came up with:
RSpec::Matchers.define :be_boolean do match do |actual| expect(actual).to be_in([true, false]) end end See: https://www.relishapp.com/rspec/rspec-expectations/v/3-4/docs/custom-matchers
It can be used like so:
it 'is boolean' do expect(true).to be_boolean end
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