Permalink
Browse files

Added tests for method 'description' of ValidateNumericalityOfMatcher…

…. Fixed issue in this method.
  • Loading branch information...
1 parent 7da0648 commit 990184f23532be742ff53000b9a54a4405bade6c @moffff committed Apr 1, 2012
@@ -78,11 +78,10 @@ def matches?(subject)
end
def description
- result = "only allow numeric values"
- [:greater_than, :less_than, :greater_than_or_equal_to, :less_than_or_equal_to, :equal_to].each do |method|
- result << "#{method} #{instance_variable_get("@#{method}")}" if instance_variable_get("@#{method}")
+ result = [:greater_than, :greater_than_or_equal_to, :less_than, :less_than_or_equal_to, :equal_to].map do |method|
+ "#{method} #{instance_variable_get("@#{method}")}" if instance_variable_get("@#{method}")
end
- result.join(', ') + " for #{@attribute}"
+ ["allow numeric values", result.compact.join(', '), "for", @attribute].join(" ")
end
private
@@ -78,6 +78,20 @@
end
end
+ context "description tests" do
+ [:greater_than, :greater_than_or_equal_to, :equal_to, :less_than, :less_than_or_equal_to].each do |parameter|
+ it "should return the correct description when the #{parameter} validation fails" do
+ validate_numericality_of(:attr).send(parameter, 0).description.should == "allow numeric values #{parameter} 0 for attr"
+ end
+ end
+
+ [:greater_than, :greater_than_or_equal_to].product([:less_than_or_equal_to, :less_than]).each do |parameters|
+ it "should return the correct description when the #{parameters.join(", ")} validations fails" do
+ validate_numericality_of(:attr).send(parameters.first, 0).send(parameters.last, 10).description.should == "allow numeric values #{parameters.first} 0, #{parameters.last} 10 for attr"
+ end
+ end
+ end
+
context "a non-numeric attribute" do
before do
@model = define_model(:example, :attr => :string).new

0 comments on commit 990184f

Please sign in to comment.