Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added very basic validation spec helpers. verrrrry basic.

  • Loading branch information...
commit 5b43cad753e14a398703d4a8cd935ed5d117eba4 1 parent 052fb84
rsl authored
View
18 lib/lucky_sneaks/common_spec_helpers.rb
@@ -13,7 +13,7 @@ def class_for(name)
#
# instance_for("foo") # => @foo
def instance_for(name)
- instance_variable_get("@#{name}")
+ instance_variable_get("@#{name.underscore}")
end
# Wraps a matcher that checks if the receiver contains an <tt>A</tt> element (link)
@@ -21,5 +21,21 @@ def instance_for(name)
def have_link_to(path)
have_tag("a[href='#{path}']")
end
+
+ # Returns dummy value for specified attribute based on the datatype expected for that
+ # attribute.
+ def dummy_value_for(instance, attribute)
+ actual = instance.send(attribute)
+ case instance.column_for_attribute(attribute).type
+ when :string, :text
+ actual == "foo" ? "bar" : "food"
+ when :integer, :float, :decimal
+ actual == 108 ? 815 : 108
+ when :boolean
+ actual ? false : true
+ when :date, :datetime, :time, :timestamp
+ actual == 1.week.ago ? 2.years.ago : 1.week.ago
+ end
+ end
end
end
View
27 lib/lucky_sneaks/model_spec_helpers.rb
@@ -166,7 +166,11 @@ def have_and_belong_to_many(models)
private
def class_or_instance
- class_for(self.class.description_text) || instance_for(self.class.description_text)
+ @model_spec_class_or_instance ||= class_for(self.class.description_text) || instance
+ end
+
+ def instance
+ @model_spec_instance ||= instance_for(self.class.description_text)
end
# These methods are designed to be used at the example group [read: "describe"] level
@@ -217,6 +221,27 @@ def it_should_have_and_belong_to_many(models)
class_or_instance.should have_and_belong_to_many(models)
end
end
+
+ # Creates an expectation that the current model being spec'd has <tt>validates_presence_of</tt>
+ # the specified attribute. Takes an optional custom message to match the one in the model's
+ # validation.
+ def it_should_validate_presence_of(attribute, message = "can't be blank")
+ it "should not be valid if #{attribute} is blank" do
+ instance.attributes = valid_attributes.except(attribute)
+ instance.errors_on(attribute).should include(message)
+ end
+ end
+
+ # Creates an expectation that the current model being spec'd doesn't allow mass-assignment
+ # of the specified attribute.
+ def it_should_not_mass_assign(attribute)
+ it "should not allow mass-assignment of #{attribute}" do
+ lambda {
+ instance.update_attributes attribute => "whatever"
+ }.should_not change(instance, attribute)
+ instance.destroy unless instance.new_record?
+ end
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.