Skip to content

Instantly share code, notes, and snippets.

@floehopper
Created February 18, 2010 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save floehopper/307648 to your computer and use it in GitHub Desktop.
Save floehopper/307648 to your computer and use it in GitHub Desktop.
# an assert_that method that accepts a Mocha parameter matcher
# based http://joe.truemesh.com/blog/000511.html by Joe Walnes
require 'test/unit'
require 'rubygems'
require 'mocha'
class MyTest < Test::Unit::TestCase
def test_equals
assert_that 2, equals(3)
end
# 1) Failure:
# test_equals(MyTest)
# method assert_that in assert_that.rb at line 30
# method test_equals in assert_that.rb at line 8
# Expected: 3; Actual: 2
def test_includes
assert_that [5,7,9], includes(2)
end
# 2) Failure:
# test_has_value(MyTest)
# method assert_that in assert_that.rb at line 30
# method test_has_value in assert_that.rb at line 16
# Expected: has_value(5); Actual: {1 => 2, 3 => 4}
def test_has_value
assert_that({1 => 2, 3 => 4}, has_value(5))
end
# 3) Failure:
# test_includes(MyTest)
# method assert_that in assert_that.rb at line 30
# method test_includes in assert_that.rb at line 12
# Expected: includes(2); Actual: [5, 7, 9]
def test_instance_of
assert_that Object.new, instance_of(Array)
end
# 4) Failure:
# test_instance_of(MyTest)
# method assert_that in assert_that.rb at line 30
# method test_instance_of in assert_that.rb at line 20
# Expected: instance_of(Array); Actual: #<Object:0x1238790>
def test_regexp_matches
assert_that "hello world", regexp_matches(/goodbye/)
end
# 5) Failure:
# test_regexp_matches(MyTest)
# method assert_that in assert_that.rb at line 30
# method test_regexp_matches in assert_that.rb at line 24
# Expected: regexp_matches(/goodbye/); Actual: 'hello world'
private
def assert_that(actual, matcher)
unless matcher.matches?([actual])
message = %{Expected: #{matcher.mocha_inspect}; Actual: #{actual.mocha_inspect}}
raise Test::Unit::AssertionFailedError.new(message)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment