工厂女孩的额外论点

发布于 2024-08-29 12:51:13 字数 337 浏览 4 评论 0原文

我需要将额外的参数传递给工厂女孩以在回调中使用。像这样的东西(但实际上更复杂):

Factory.define :blog do |blog|
  blog.name "Blah"

  blog.after_create do |blog|
    blog.posts += sample_posts
    blog.save!
  end
end

然后用这样的东西创建它:

Factory.create(:blog, :sample_posts => [post1, post2])

有什么想法如何做到这一点?

I need to pass extra arguments to factory girl to be used in a callback. Something like this (but more complex really):

Factory.define :blog do |blog|
  blog.name "Blah"

  blog.after_create do |blog|
    blog.posts += sample_posts
    blog.save!
  end
end

and then create it with something like this:

Factory.create(:blog, :sample_posts => [post1, post2])

Any ideas how to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

沉默的熊 2024-09-05 12:51:14

由于瞬态属性,现在可以在没有任何“黑客”的情况下实现这一点(请参阅问题 # 的评论49)

示例:

FactoryGirl.define do
  factory :user do
    transient do
      bar_extension false
    end
    name {"foo #{' bar' if bar_extension}"}
  end
end

# Factory(:user).name = "foo"
# Factory(:user, :bar_extension => true).name = "foo bar"

对于 Factory Girl 版本 < 5.0:

FactoryGirl.define do
  factory :user do
    ignore do
      bar_extension false
    end
    name {"foo #{' bar' if bar_extension}"}
  end
end

# Factory(:user).name = "foo"
# Factory(:user, :bar_extension => true).name = "foo bar"

This is now possible without any "hacks" thanks to transient attributes (see comment on issue #49)

example:

FactoryGirl.define do
  factory :user do
    transient do
      bar_extension false
    end
    name {"foo #{' bar' if bar_extension}"}
  end
end

# Factory(:user).name = "foo"
# Factory(:user, :bar_extension => true).name = "foo bar"

For Factory Girl versions < 5.0:

FactoryGirl.define do
  factory :user do
    ignore do
      bar_extension false
    end
    name {"foo #{' bar' if bar_extension}"}
  end
end

# Factory(:user).name = "foo"
# Factory(:user, :bar_extension => true).name = "foo bar"
徒留西风 2024-09-05 12:51:14

一种选择是为 after_create 挂钩检查的额外帖子创建一个虚拟访问器:

class Blog
  has_many :posts
  attr_accessible :name, :title, ... # DB columns
  attr_accessor   :sample_posts      # virtual column
end

Factory.define :blog do |blog|
  blog.name 'Blah'

  blog.after_create do |b|
    b.posts += b.sample_posts
    b.save!
  end
end

Factory(:blog, :sample_posts => [post1, post2])

One option would be to create a virtual accessor for extra posts that the after_create hook checks:

class Blog
  has_many :posts
  attr_accessible :name, :title, ... # DB columns
  attr_accessor   :sample_posts      # virtual column
end

Factory.define :blog do |blog|
  blog.name 'Blah'

  blog.after_create do |b|
    b.posts += b.sample_posts
    b.save!
  end
end

Factory(:blog, :sample_posts => [post1, post2])
路还长,别太狂 2024-09-05 12:51:14

显然,如果没有需要修改模型本身的解决方法,目前这是不可能的。此错误报告于:http://github.com/thoughtbot/factory_girl/issues#问题/49

Apparently, this is not possible at the moment without workarounds that require modifying the model itself. This bug is reported in: http://github.com/thoughtbot/factory_girl/issues#issue/49

画中仙 2024-09-05 12:51:14

另一种选择是使用 build 而不是 create 并将 :autosave 添加到集合中:

class Blog
  has_many :posts, :autosave => true
end

Factory.define :blog do |blog|
  blog.name 'Blah'
  blog.posts { |_| [Factory.build(:post)] }
end

Factory(:blog, :posts => [post1, post2])
#or
Factory.build(:blog, :posts => [unsavedPost1, unsavedPost2])

Another option would be to use build instead of create and add :autosave to the collection:

class Blog
  has_many :posts, :autosave => true
end

Factory.define :blog do |blog|
  blog.name 'Blah'
  blog.posts { |_| [Factory.build(:post)] }
end

Factory(:blog, :posts => [post1, post2])
#or
Factory.build(:blog, :posts => [unsavedPost1, unsavedPost2])
早茶月光 2024-09-05 12:51:14

如果您要在 Factorygirl 文件中打开课程,我建议这样做,

require "user"
class User
  attr :post_count
end

以便您打开课程,而不是覆盖它

if you are opening the class inside the factorygirl file, i suggest doing it like

require "user"
class User
  attr :post_count
end

so that you are opening the class, instead of overwriting it

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