Created
April 30, 2009 06:12
-
-
Save wezm/104291 to your computer and use it in GitHub Desktop.
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
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