我的工厂创建顺序有问题

发布于 2024-09-19 01:19:15 字数 1313 浏览 11 评论 0原文

我希望有人能发现为什么这行不通。

我收到一个正在调用的错误,因为我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宣告ˉ结束 2024-09-26 01:19:15

您有一个 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 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文