I was hoping someone would spot why this wouldn't work.
I am getting an error thats being called because the attributes I specify with开发者_开发问答 Factory_Girl are not being applied to the stub before validation.
The Error:
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
Factories
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 - Model
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
Also, feel free to nitpick any blatantly gross looking code. :D
Thanks!!
You have a category
attribute, which appears to be a string, but you also seem to have a category association which automatically creates, among other things, an attribute on Post called category
, probably overwriting your category attribute. Hence, the Category
class has no downcase
method, because it's not a String.
Rename your category attribute to something like category_name
, but really you shouldn't have that attribute at all.
Maybe where you're calling self.category.downcase
you meant self.category.name.downcase
?
精彩评论