How do i cover the below method using Rspec?
def validate
if !self.response.blank? && !self.response.match("<").nil?
self.errors.add :base, 'Please ensure that Response field do not contain HTML(< and >) tags'
end
end
Can anybody help?
How do i cover the below method using Rspec?
def validate
if !self.response.blank? && !self.response.match("<").nil?
self.errors.add :base, 'Please ensure that Response field do not contain HTML(< and >) tags'
end
end
Can anybody help?
It appears from the code that what you want is to validate the response
attribute and set an error message if invalid.
So assuming your model is named Post:
context "HTML tags in response" do
before(:each) do
@post = Post.new(:response => "<")
end
it "should not be valid" do
@post.should_not be_valid
end
it "should set the error hash" do
@post.errors.should include('Please ensure that Response field do not contain HTML(< and >) tags')
end
end
You should check for the desired behavior of the model, not the implementation. It shouldn't matter whether the validation is happening in a custom method, or in Rails built-in validation routines.
As a side note, it's generally better to add the error message to the attribute rather than errors.base
. So you might say instead:
self.errors.add(:response, "etc. etc.")