我希望有人會發現爲什麼這不起作用。我的工廠創建順序有問題
我得到一個錯誤,因爲我用Factory_Girl指定的屬性沒有在驗證前應用於存根。
的錯誤:
undefined method `downcase' for #<Category:0x1056f2f60>
RSpec2
it "should vote up" do
@mock_vote = Factory.create(:vote)
Vote.stub(:get_vote).and_return(@mock_vote)
get :vote_up, :id => "1"
end
工廠
Factory.define :vote, :class => Vote do |v|
v.user_id "1"
v.association :post
end
Factory.define :post, :class => Post do |p|
p.category "spirituality"
p.name "sleezy snail potluck"
p.association :category
end
Factory.define :category, :class => Category do |c|
c.name "spirituality"
c.id "37"
end
Post.rb - 型號
before_save :prepare_posts
validate :category?
def prepare_posts
self.update_attribute("category", self.category.downcase)
if self.url?
self.url = "http://" + self.url unless self.url.match /^(https?|ftp):\/\//
end
end
def category?
unless Category.exists?(:name => self.category.downcase)
errors.add(:category, "There's no categories with that name.")
end
return true
end
而且,隨意挑剔任何公然總值看代碼。 :D
謝謝!!
至於nitpicks,':class => Vote/Post/Category'是多餘的,因爲factory_girl會從你的工廠名稱推導出類(:vote成爲投票,:post變成post等) – 2010-09-13 13:08:49
啊也這麼覺得!希望他們的文檔說。謝謝! – Trip 2010-09-13 17:58:45