非 ActiveRecord 模型的机械师

发布于 2024-09-13 17:34:13 字数 128 浏览 3 评论 0原文

是否可以使用 Machinist 为非 activerecord 模型创建蓝图?无论我尝试什么似乎都会产生错误!

如果这对 Machinist 来说是不可能的,是否有其他夹具生成宝石可以做到这一点?我还读到工厂女孩对此有疑问。

Is it possible to use Machinist to create blueprints for non-activerecord models? Seems to generate an error no matter what I try!

If this is impossible with Machinist, is there another fixture generating gem that can do it? I've also read that Factory Girl has issues with this.

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

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

发布评论

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

评论(2

她说她爱他 2024-09-20 17:34:13

我对 Machinist 2 beta 1 做了一些调查,看起来它支持普通的 ruby​​ 对象。这就是我为让它发挥作用所做的事情。很高兴了解任何更简单的解决方案。

require 'rubygems'
require 'machinist'
require 'faker'

class YourObject
 attr_accessor :field1, :field2
end


# For all Objects
class Object
 extend Machinist::Machinable

 def self.blueprint_class
   Machinist::Blueprint
 end
end   

# Or just one object
YourObject.send(:extend, Machinist::Machinable)
YourObject.class_eval do
 def self.blueprint_class
   Machinist::Blueprint
 end
end



YourObject.blueprint do
 field1 { rand(1000) }
 field2 { Faker::Name }
end

obj = YourObject.make

I did a little bit of investigating into Machinist 2 beta 1 and it looks like it supports plain ruby objects. Here's what I did to get it working. Happy to learn of any simpler solutions.

require 'rubygems'
require 'machinist'
require 'faker'

class YourObject
 attr_accessor :field1, :field2
end


# For all Objects
class Object
 extend Machinist::Machinable

 def self.blueprint_class
   Machinist::Blueprint
 end
end   

# Or just one object
YourObject.send(:extend, Machinist::Machinable)
YourObject.class_eval do
 def self.blueprint_class
   Machinist::Blueprint
 end
end



YourObject.blueprint do
 field1 { rand(1000) }
 field2 { Faker::Name }
end

obj = YourObject.make
梦太阳 2024-09-20 17:34:13

如果有人好奇的话,FactoryGirl 和 PORO 的问题之一(可能还有其他问题)是它不处理带参数的构造函数。您可以根据此处的答案进行支付。

In case anyone's curious, one of the problems (there may be others) with FactoryGirl and POROs is that it doesn't handle constructors with arguments. You can make due per the answer here.

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