0

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

Jason Nerer
  • 551
  • 7
  • 19

2 Answers2

0

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')
shingara
  • 46,608
  • 11
  • 99
  • 105
  • shingara, I know, thats the way I'm using it. But the question ist, if one can use named blueprints like: User.blueprint(:no_discount_user) do and User.blueprint(:discount_user) do so that I can call: User(:discount_user).make to create a user with a discountrate and User(:no_discount_user).make to create a user without. It would make Cucumber step definition much dryer in my case. But it seems I need to switch to machinist instead. – Jason Nerer May 05 '10 at 22:14
  • Yes I think it's not possible. The extension of factory_girl can't made that – shingara May 06 '10 at 09:01
0

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

Alexey
  • 9,197
  • 5
  • 64
  • 76