Skip to content

Instantly share code, notes, and snippets.

@kerryb
Created Feb 18, 2009
Embed
What would you like to do?
Asserting true and false (ish) in rspec
# If you want to check that something's true or false in the normal ruby sense
# (ie anything other than false or nil is true), you can use double negation
# to force it into an actual boolean (eg !!foo.should be_true). To keep this
# ugliness out of your tests, stick these matchers in your spec helper:
def be_truish
return simple_matcher do |obj, matcher|
matcher.description = 'be true(ish)'
matcher.failure_message = "Expected #{obj.inspect} to be true(ish)"
matcher.negative_failure_message = "Expected #{obj.inspect} not to be true(ish)"
!!obj == true
end
end
def be_falsish
return simple_matcher do |obj, matcher|
matcher.description = 'be false(ish)'
matcher.failure_message = "Expected #{obj.inspect} to be false(ish)"
matcher.negative_failure_message = "Expected #{obj.inspect} not to be false(ish)"
!!obj == false
end
end
# Test cases:
describe '-ish matchers' do
it {1.should be_truish}
it {'foo'.should be_truish}
it {[].should be_truish}
it {true.should be_truish}
it {false.should_not be_truish}
it {nil.should_not be_truish}
it {false.should be_falsish}
it {nil.should be_falsish}
it {1.should_not be_falsish}
it {'foo'.should_not be_falsish}
it {[].should_not be_falsish}
it {true.should_not be_falsish}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment