factory girl passing arguments to model definition on build/create
Asked Answered
C

2

16

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)>'
Crackerbarrel answered 31/7, 2013 at 10:30 Comment(0)
B
23

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
Blount answered 31/7, 2013 at 10:48 Comment(2)
It should be noted that only since FactoryGirl 2.5 has initialize_with been supported: robots.thoughtbot.com/factory-girl-2-5-gets-custom-constructors (which only really matters if you're stuck using rspec 1.x due to dependency issues)Ferroconcrete
Also note: DEPRECATION WARNING: `#ignore` is deprecated and will be removed in 5.0. Please use `#transient` instead.Geralyngeraniaceous
S
0

It's because you have a constructor with mandatory arguments. You have a few options;

1) Make the argument non-mandatory (although this would mean you're changing your code to suit your tests - naughty!)

def initialize(message = nil)

2) Use the "initialize_with" syntax in your factory;

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

  initialize_with { new(message) }
end
Sweptback answered 26/2, 2014 at 9:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.