Created
February 18, 2009 21:23
-
-
Save kerryb/66557 to your computer and use it in GitHub Desktop.
Asserting true and false (ish) in rspec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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