我的工厂创建顺序有问题
我希望有人能发现为什么这行不通。
我收到一个正在调用的错误,因为我使用 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
谢谢!!
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!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有一个
category
属性,它看起来是一个字符串,但您似乎也有一个类别关联,它会自动在 Post 上创建一个名为category
的属性。 ,可能会覆盖您的类别属性。因此,Category
类没有downcase
方法,因为它不是字符串。将您的类别属性重命名为
category_name
之类的名称,但实际上您根本不应该拥有该属性。也许您在调用
self.category.downcase
时指的是self.category.name.downcase
?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 calledcategory
, probably overwriting your category attribute. Hence, theCategory
class has nodowncase
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 meantself.category.name.downcase
?