工厂女孩:如何建造一个不受模型束缚的工厂?

发布于 2024-11-17 21:15:51 字数 342 浏览 2 评论 0原文

我只需要信用卡和地址等属性的哈希值。 示例:

Factory.define :credit_card, :class => Object do |c|
  c.first_name "Alice"
  c.last_name "Liddel"
  c.month "May"
  c.year { Time.now.year + 1 }
  c.number "1234567812345678"
  c.type "Visa"
  c.verification_value "123"
end

显然,对象没有任何属性,而且我没有credit_cord 对象...我只需要一个标准的信用卡骨架。

I just need a hash of attributes for things like credit card, and address.
Example:

Factory.define :credit_card, :class => Object do |c|
  c.first_name "Alice"
  c.last_name "Liddel"
  c.month "May"
  c.year { Time.now.year + 1 }
  c.number "1234567812345678"
  c.type "Visa"
  c.verification_value "123"
end

obviously, object doesn't have any attributes, and I don't have a credit_cord object... I just need a standard credit card skeleton.

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

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

发布评论

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

评论(1

故事和酒 2024-11-24 21:15:51

使用工厂女孩来做这件事有什么好处?辅助方法怎么样:

def credit_card(attrs = {})
   {
     :first_name => "Alice",
     ...,
     :verification_value => "123"
   }.with_indifferent_access.merge(attrs)
end

credit_card :first_name => "Linda"   # returns { :first_name => "Linda", :last_name => "Liddel", ... }

What is the advantage of using factory girl for this? How about a helper method:

def credit_card(attrs = {})
   {
     :first_name => "Alice",
     ...,
     :verification_value => "123"
   }.with_indifferent_access.merge(attrs)
end

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