I have the following to remove the spaces on a specific attribute.
#before_validation :strip_whitespace
protected
def strip_whitespace
self.title = self.title.strip
end
And I want to test it. For now, I’ve tried:
it "shouldn't create a new part with title beggining with space" do
@part = Part.new(@attr.merge(:title => " Test"))
@part.title.should.eql?("Test")
end
What am I missing?
Validations won’t run until the object is saved, or you invoke
valid?manually. Yourbefore_validationcallback isn’t being run in your current example because your validations are never checked. In your test I would suggest that you run@part.valid?before checking that the title is changed to what you expect it to be.app/models/part.rb
spec/models/part_spec.rb
This will pass when the validation is included, and fails when the validation is commented out.