Factory Girl after_build 回调并覆盖 rspec 中的问题

发布于 2024-11-26 09:37:49 字数 1162 浏览 0 评论 0原文

我们在使用 Factory Girl 的新 after_build 回调并让覆盖参数正常工作时遇到问题。我们将模型定义为:

factory :widget do
  name "Widget Name"
  position 1
  creator
  content_type "text_content"
  content "This is the content"
  change_comment "This is the change comment"
  after_build do |widget|
    widget.page = Factory.create(:page)
    widget.canvas = widget.page.canvas
  end
end

请注意,本例中的页面和小部件都需要画布;一个小部件可以选择有一个页面。我们希望工厂构建属于页面的小部件,并且该小部件和页面都属于同一画布。

我们有一个 rspec 测试,需要测试没有有效的画布会使小部件模型无效,

context "Canvas" do
    it "should be required" do
        widget = Factory.build(:widget, :canvas => nil)
        widget.should_not be_valid
    end
end

即 Factory.build(:widget, :canvas => nil) 不应该有效,因为画布以 nil 传递,但是 after_build方法忽略这一点并仍然将画布应用于模型。

注意:我们可以通过将 rspec 测试更改为以下内容来解决此问题,其中我们创建一个有效的模型,然后将画布设置为 nil 作为第二步,这确实可以正常工作,但

context "Canvas" do
    it "should be required" do
        widget = Factory.build(:widget)
        widget.canvas = nil
        widget.should_not be_valid
    end
end

感觉我们已经丢失了一些这是工厂女孩的核心功能,所以我们希望我们的工厂女孩​​模型设置中缺少一些东西,这些东西可以让我们更正确地完成这个任务。非常感谢人们提供的任何见解!

We're having a problem with Factory Girl's new after_build callback and getting the override parameters to work. we've defined a model as:

factory :widget do
  name "Widget Name"
  position 1
  creator
  content_type "text_content"
  content "This is the content"
  change_comment "This is the change comment"
  after_build do |widget|
    widget.page = Factory.create(:page)
    widget.canvas = widget.page.canvas
  end
end

note that a page and a widget in this example both require a canvas; a widget can optionally have a page. we want the factory to build the widget belonging to a page, and both the widget and page belong to the same canvas.

we have an rspec test that needs to test that not having valid canvas makes the widget model invalid

context "Canvas" do
    it "should be required" do
        widget = Factory.build(:widget, :canvas => nil)
        widget.should_not be_valid
    end
end

i.e. Factory.build(:widget, :canvas => nil) should not be valid as the canvas is being passed in nil, however the after_build method is ignoring this and still applying a canvas to the model.

Note: we can work-around this issue by changing the rspec test to the following, where we create a valid model then set the canvas to nil as a 2nd step and this does work correctly i.e.

context "Canvas" do
    it "should be required" do
        widget = Factory.build(:widget)
        widget.canvas = nil
        widget.should_not be_valid
    end
end

however this feels like we've lost some of the core functionality of Factory Girl, so we're hopeful we're missing something in our factory girl model set up that will let us accomplish this more correctly. Greatly appreciate any insights people can provide!

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

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

发布评论

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

评论(1

柠檬心 2024-12-03 09:37:49

我认为我无法对这个问题发表评论,因此我将在此处发布对您的代码略有不同的问题的修复。

我认为你的小部件工厂将始终创建一个新的页面/画布,即使你将非零页面/画布传递到 Factory.build/Factory.create 中。这是一个解决方案,除了零/错误的情况:

after_build do |widget|
  widget.page   ||= Factory.create(:page)
  widget.canvas ||= widget.page.canvas
end

我仍在寻找错误情况的解决方案......

I don't think I can comment on the question, so I will post a fix to a slightly-different issue with your code here.

I think your widget factory will always create a new page/canvas, even if you pass a non-nil page/canvas into Factory.build/Factory.create. Here's a solution for that, except for the nil/false cases:

after_build do |widget|
  widget.page   ||= Factory.create(:page)
  widget.canvas ||= widget.page.canvas
end

I'm still looking for a solution for the falsey cases...

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