Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec: How to test for a boolean return type?

Tags:

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.

like image 769
Dogweather Avatar asked Nov 14 '12 01:11

Dogweather


2 Answers

expect(variable).to be_in([true, false]) 
like image 123
Tomasz Kapłon Avatar answered Sep 28 '22 10:09

Tomasz Kapłon


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 
like image 28
Rimian Avatar answered Sep 28 '22 12:09

Rimian