Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added tests for method 'description' of ValidateNumericalityOfMatcher…

…. Fixed issue in this method.
  • Loading branch information...
commit 990184f23532be742ff53000b9a54a4405bade6c 1 parent 7da0648
@moffff authored
View
7 lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb
@@ -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
View
14 spec/shoulda/active_model/validate_numericality_of_matcher_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.