Skip to content

Instantly share code, notes, and snippets.

@wezm
Created April 30, 2009 06:12
Show Gist options
  • Save wezm/104291 to your computer and use it in GitHub Desktop.
Save wezm/104291 to your computer and use it in GitHub Desktop.
module Spec
module Mocks
# module ArgumentMatchers # rspec > 1.1.12
module ArgumentConstraints
# Based on:
# http://trottercashion.com/2009/2/12/custom-argumentmatchers-in-rspec
class NumericNearMatcher
def initialize(expected, tolerance)
@expected = expected
@tolerance = tolerance
end
# actual is the value passed by the test. Its expected to respond to
# between?, like that defined in Comparable, if not return false
def ==(actual)
actual.between?(@expected - @tolerance, @expected + @tolerance)
rescue NoMethodError
false
end
def description
"numeric_near(#{@expected}, +/- #{@tolerance})"
end
end
# This is the user interface that allows things like
# obj.should_receive(:msg).with(a, numeric_near(1.23, 0.01))
def numeric_near(*args)
NumericNearMatcher.new(*args)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment