使用factory_girl命名蓝图

发布于 2024-08-31 03:27:47 字数 752 浏览 12 评论 0原文

我正在使用 Factory Girl,但喜欢机械师语法。所以我想知道,是否有任何方法为类创建一个命名蓝图,以便我可以拥有类似的东西:

User.blueprint(:no_discount_user) do
  admin           false
  hashed_password "226bc1eca359a09f5f1b96e26efeb4bb1aeae383"
  is_trader       false
  name            "foolish"
  salt            "21746899800.223524289203464"
end

User.blueprint(:discount_user) do
  admin           false
  hashed_password "226bc1eca359a09f5f1b96e26efeb4bb1aeae383"
  is_trader       true
  name            "deadbeef"
  salt            "21746899800.223524289203464"
  discount_rate { DiscountRate.make(:rate => 20.00) }
end

DiscountRate.blueprint do
  rate {10}
  not_before ...
  not_after ...
end

有没有一种方法可以使用机械师语法来制作factory_girl?我没有找到。帮助表示赞赏。

提前谢谢 贾森

I am using Factory Girl but like the machinist syntax. So I wonder, if there is any way creating a named blueprint for class, so that I can have something like that:

User.blueprint(:no_discount_user) do
  admin           false
  hashed_password "226bc1eca359a09f5f1b96e26efeb4bb1aeae383"
  is_trader       false
  name            "foolish"
  salt            "21746899800.223524289203464"
end

User.blueprint(:discount_user) do
  admin           false
  hashed_password "226bc1eca359a09f5f1b96e26efeb4bb1aeae383"
  is_trader       true
  name            "deadbeef"
  salt            "21746899800.223524289203464"
  discount_rate { DiscountRate.make(:rate => 20.00) }
end

DiscountRate.blueprint do
  rate {10}
  not_before ...
  not_after ...
end

Is there a way making factory_girl with machinist syntax acting like that? I did not find one. Help appreciated.

Thx in advance
Jason

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

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

发布评论

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

评论(2

旧时模样 2024-09-07 03:27:47

是的,你可以。需要蓝图语法

  require 'factory_girl/syntax/blueprint'
  Sham.email {|n| "#{n}@example.com" }

  User.blueprint do
    name  { 'Billy Bob' }
    email { Sham.email }
  end

  User.make(:name => 'Johnny')

Yes you can. require blueprint syntaxe

  require 'factory_girl/syntax/blueprint'
  Sham.email {|n| "#{n}@example.com" }

  User.blueprint do
    name  { 'Billy Bob' }
    email { Sham.email }
  end

  User.make(:name => 'Johnny')
梦罢 2024-09-07 03:27:47

如果您担心测试的干燥性,您可以考虑我创建的active_factory 插件。在其中,您可以像这样定义一个工厂:

factory :discount_user, :class => User do
  admin           false
  hashed_password "226bc1eca359a09f5f1b96e26efeb4bb1aeae383"
  is_trader       true
  name            "deadbeef"
  salt            "21746899800.223524289203464"
  discount_rate { DiscountRate.make(:rate => 20.00) }
end

另一个选项是在测试中“添加折扣”:

models { discount_rate - user - ... }

它将在两个模型之间创建关联。因此,您可以保持规格干燥,同时避免创建大量工厂。

抱歉,如果我没有准确回答您的问题

If you concerned with DRYness of your tests, you may consider active_factory plugin, that i've created. In it you could define a factory like this:

factory :discount_user, :class => User do
  admin           false
  hashed_password "226bc1eca359a09f5f1b96e26efeb4bb1aeae383"
  is_trader       true
  name            "deadbeef"
  salt            "21746899800.223524289203464"
  discount_rate { DiscountRate.make(:rate => 20.00) }
end

Another option would be just 'add discount' inside your test:

models { discount_rate - user - ... }

It would create association between the two models. Thus you can keep your specs DRY while avoiding creating a lot of factories.

Sorry, if I'm not answering exactly your question

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