Skip to content

Instantly share code, notes, and snippets.

@ivan22
Created May 17, 2014 03:17
Show Gist options
  • Select an option

  • Save ivan22/b48aa1416ec7cd1561b8 to your computer and use it in GitHub Desktop.

Select an option

Save ivan22/b48aa1416ec7cd1561b8 to your computer and use it in GitHub Desktop.
Factory Girl

Offical documentation: https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md

Gemfile

gem 'factory_girl_rails'

Convenience

  • Add the following to spec/spec_helper.rb for easier calling of FactoryGirl methods
RSpec.configure do |config|
  # For Factory Girl
  config.include FactoryGirl::Syntax::Methods
end

Using without models

  • FactoryGirl creates doubles based on ActiveRecords
  • If you don't have model, you can fake one:
#spec/support/models/deal.rb
class  Deal
   attr_accessor  :title, :remainingQuantity, :price_amount
end

Factories for subclasses

FactoryGirl.define do
  factory :user do
    sequence(:email) { |n| "user#{n}@example.com" }
    password "123123"
    confirmed_at Date.today

    factory :buyer, class: Buyer do
      first_name "Jay"
      last_name "Chou"
      address "Taiwan"
      mobile "89933-3333"
      telephone "2887-22232"
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment