我希望有人会发现为什么这不起作用。
我收到一个错误,因为我使用 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
谢谢!!