热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何引用我在制作器中制作的对象?-HowtorefertotheobjectI'mFabricatinginsidemyFabricator?

ImusingRuby2.1.1p76andRails4.0.4andtheFabricationgem.我使用的是Ruby2.1.1p76和Rails4.0.4以及这个制

I'm using Ruby 2.1.1p76 and Rails 4.0.4 and the Fabrication gem.

我使用的是Ruby 2.1.1p76和Rails 4.0.4以及这个制作gem。

Is it possible to refer to the object currently being fabricated?

是否有可能提及目前正在制造的物体?

I have a class Foo and a class Bar. I have fabricators for each. My problem is that each of class Foo and Bar contain a field that refers to the other class:

我有一个类Foo和一个类Bar。我每个人都有制造者。我的问题是每一个类Foo和Bar都包含一个引用其他类的字段:

class Foo 

class Bar 

It's bothersome to have to fabricate one, then the other and then set the reference for the first in my specs:

制造一个,然后制造另一个,然后在我的规格中为第一个设定参考,这很麻烦:

let!( :foo ) { Fabricate( :foo ) }
let!( :bar ) { Fabricate( :bar, foo: foo ) }

before( :each ) do
  foo.update( current_bar: bar )
end 

I'd much rather just fabricate a Foo and have its current_bar fabricated and already referring to the Foo I'm fabricating. I've read through the fabrication gem documentation and I can't find any way that this is possible. I may just be overlooking it. Does anyone know of a way to accomplish this?

我宁愿制作一个Foo,并让它的current_bar被制作并且已经指向我正在制作的Foo。我已经阅读了《制造gem》的文档,我找不到任何可能的方法。我可能只是忽略了它。有人知道有什么方法可以做到这一点吗?

For completeness -- fabricators:

出于完整性的考虑——制造商:

Fabricator( :foo ) do
  current_bar nil
end

Fabricator( :bar ) do
  foo
end

1 个解决方案

#1


4  

Yup. overlooked it in the documentation.

是的。在文档中忽略了它。


You can define them in your Fabricators as a block that optionally receives the object being fabricated and a hash of any transient attributes defined. As with anything that works in the Fabricator, you can also define them when you call Fabricate and they will work just like you’d expect. The callbacks are also stackable, meaning that you can declare multiple of the same type in a fabricator and they will not be clobbered when you inherit another fabricator.

您可以将它们定义为可选地接收正在构建的对象的块,以及定义的任何暂态属性的散列。与在Fabricator中工作的任何东西一样,您也可以在调用Fabricate时定义它们,它们将像您期望的那样工作。回调也是可堆叠的,这意味着您可以在fabricator中声明多个相同类型的回调,并且当您继承另一个fabricator时,它们不会被阻塞。

Fabricator(:place) do
  before_validation { |place, transients| place.geolocate! }
  after_create { |place, transients| Fabricate(:restaurant, place: place) }
end

Also, in my case, I needed to use the after_save callback. I was able set the current_bar on my foo object inside the fabricator, but once in the spec, the current_bar was still nil. The update method isn't available inside after_create (I'm new to ruby so I'm not sure why), but it is available inside after_save. Calling update got me going.

同样,在我的例子中,我需要使用after_save回调。我可以在fabricator中设置foo对象的current_bar,但是在规范中,current_bar仍然是nil。更新方法在after_create中不可用(我是ruby的新手,所以不知道为什么),但在after_save中可以用。电话更新让我开始工作。

Fabricator(:foo) do

  transient :current_bar_data

  after_save { |foo, transients|
    bar = Fabricate( :bar, foo: foo, bar_data: transients[ :current_bar_data ] )
    foo.update( current_bar: bar )
  }

  current_bar nil
end

Now I can fabricate a foo complete with current_bar for my specs:

现在我可以用current_bar制作一个完整的foo:

let!( :some_foo ) { Fabricate( :foo, current_bar_data: "some bar data" ) }

推荐阅读
author-avatar
1021365712_3a478e
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有