<?xml version="1.0" encoding="UTF-8"?>
<wiki>
  <body>&lt;p&gt;This is how we have things set up for our Rails projects (with RSpec and Cucumber goodness).&lt;/p&gt;
&lt;p&gt;Currently (as of version 0.1.3) there is a bug when ObjectFactory is used as a gem &amp;#8211; so we install it as a plugin (&lt;tt&gt;cd vendor/plugins &amp;amp;&amp;amp; gem unpack brightbox-object-factory&lt;/tt&gt; does the trick).  However, I promise I &lt;strong&gt;will&lt;/strong&gt; get this fixed very soon.&lt;/p&gt;
&lt;h2&gt;Preparation&lt;/h2&gt;
&lt;p&gt;We add a setup file to the lib folder: &lt;tt&gt;lib/prepare_object_factory.rb&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;This contains a function that prepares the ObjectFactory for use:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
def prepare_object_factory
  when_creating_a Company, 
    :auto_generate =&amp;gt; [:name, :address, :city, :postcode, :telephone]

  when_creating_a Person, 
    :auto_generate =&amp;gt; [:first_name, :last_name], 
    :auto_confirm =&amp;gt; :password, 
    :generate_email_address =&amp;gt; :email, 
    :set =&amp;gt; { :age =&amp;gt; 22 }, 
    :generate =&amp;gt; { :company =&amp;gt; lambda { a_saved Company } }
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note the use of :generate and lambda to ensure that people are linked to a (dynamically created) Company (unless you explicitly override it at creation-time).&lt;/p&gt;
&lt;h2&gt;RSpec&lt;/h2&gt;
&lt;p&gt;We then add the following to our &lt;tt&gt;spec/spec_helper.rb&lt;/tt&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
require 'object_factory'
require 'lib/prepare_object_factory'

Spec::Runner.configure do |config|
  # various bits of config
  config.before :all do 
    prepare_object_factory
  end
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This makes sure that the factory is prepared before running the specs.&lt;/p&gt;
&lt;p&gt;You can then use it in your specs as follows:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
describe Person do
  it &quot;should have a first name&quot; do
    @person = a Person, :first_name =&amp;gt; ''
    @person.should_not be_valid
    @person.should have(1).errors_on(:first_name)
  end
  it &quot;should belong to a company&quot; do
    @person = a Person, :company =&amp;gt; nil
    @person.should_not be_valid
    @person.should have(1).errors_on(:company)
  end
  # und so weiter
end
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Cucumber&lt;/h2&gt;
&lt;p&gt;We add the following to &lt;tt&gt;features/support/env.rb&lt;/tt&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
require 'object_factory'
require 'lib/prepare_object_factory'

prepare_object_factory
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And we use it in our steps as follows:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
Given /^someone called (.*)$/ do | first_name | 
  person = a_saved Person, :first_name =&amp;gt; first_name
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Pretty simple huh?&lt;/p&gt;</body>
  <created-at type="datetime">2010-02-09T19:18:02-08:00</created-at>
  <id type="integer">152459</id>
  <permalink>usage-with-rails-rspec-and-cucumber</permalink>
  <repository-id type="integer">123011</repository-id>
  <title>Usage with Rails, RSpec and Cucumber</title>
  <updated-at type="datetime">2009-03-05T13:22:15-08:00</updated-at>
  <user-id type="integer">1154</user-id>
</wiki>
