Just starting out here, I have a Article.rb model that stores articles.
So it has the article_body, title, etc. as properties.
开发者_开发知识库I need to clean the title for bad characters, should I create a function that takes the title and returns the cleaned up title string in the Article.rb model?
I want to write a rpsec test for this also, preferrable TDD style so I need some guidance.
I have so far:
describle Article do
before(:each) do
a = Article.new
end
it 'should remove any commas from the title' do
end
end
You would probably want to take advantage of model callbacks such as before_save.
class Article < ActiveRecord::Base
before_save :strip_commas
def strip_commas
self.title.gsub!(",", "")
end
end
This will strip the commas before the save.
You would then want to do something like:
describle Article do
before(:each) do
article = Article.new(:title => "My title")
end
it 'should remove any commas from the title' do
article.title = "My title, contains, commas"
article.save
article.title.should eq("My title contains commas")
end
end
Something like this:
describle Article do before(:each) do article = Article.new(:title => " variables , methods , instances") end it 'should remove any commas from the title' do article.your_method article.title.index(',').should be_nil end end
精彩评论