factory girl passing arguments to model definition on build/create

Furqan Asghar picture Furqan Asghar · Jul 31, 2013 · Viewed 23.5k times · Source

models/message.rb

class Message

  attr_reader :bundle_id, :order_id, :order_number, :event

  def initialize(message)
    hash = message
    @bundle_id = hash[:payload][:bundle_id]
    @order_id  = hash[:payload][:order_id]
    @order_number = hash[:payload][:order_number]
    @event = hash[:concern]
  end
end

spec/models/message_spec.rb

require 'spec_helper'

describe Message do
  it 'should save the payload' do
    payload = {:payload=>{:order_id=>138251, :order_number=>"AW116554416"}, :concern=>"order_create"}
    message = FactoryGirl.build(:message, {:payload=>{:order_id=>138251, :order_number=>"AW116554416"}, :concern=>"order_create"})
    message.event.should == "order_create"
  end
end

error_log

Failures:

1) Message should save the payload

 Failure/Error: message = FactoryGirl.build(:message, {:payload=>{:order_id=>138251, :order_number=>"AW116554416"}, :concern=>"order_create"})
 ArgumentError:
   wrong number of arguments (0 for 1)
 # ./app/models/message.rb:4:in `initialize'
 # ./spec/models/message_spec.rb:7:in `block (2 levels) in <top (required)>'

Answer

cryo28 picture cryo28 · Jul 31, 2013

FactoryGirl requires you to define factory first. Let's say in a file spec/factories/messages.rb:

FactoryGirl.define do
  factory :message do
    bundle_id 1
    order_id 2
    ...etc...
  end
end

After this you'll be able to invoke factory build create like this:

FactoryGirl.build(:message)  # => bundle_id == 1, order_id == 2
FactoryGirl.build(:message, order_id: 3) # => bundle_id == 1, order_id == 3

However, there is one problem in your particular case. FactoryGirl's default builders operate on top of ActiveRecord-alike interface. It sets defined attributes through setters, not through a hash of attrs passed to the model constructor:

m = Message.new
m.bundle_id = 1
m.order_id  = 2

So you have to create a custom constructor to work with the interface of your model (which doesn't conform to ActiveRecord-alike model) and register it in your factory definition. See factory girl docs for details.

Let me show you an example of doing so. Sorry I didn't test it but it should give you a clue:

FactoryGirl.define do
  factory :message do
    ignore do
      # need to make all attributes transient to avoid FactoryGirl calling setters after object initialization
      bundle_id 1
      order_id 2
    end

    initialize_with do
      new(payload: attributes)
    end
  end
end