thoughtbot/factory_girl — Issue #290: Undefined method error raised when creating instance
Author: katragadda
Created: 2012-02-07 13:01:41+00:00
State: open

Calling FactoryGirl.create with trait raises error when creating the instance. It fails to find method 'strip', which seems like a factory_girl gem issue not code.

The error disappears if I dont' pass the trait or use FactoryGirl.build. But I need to be able to create the object to test the model relationships.

# private method in user_details table
def sanitize_data
self.name = self.name.strip unless self.name.blank?
self.value = self.value.strip unless self.value.blank?
end

# Factory call in rspec
FactoryGirl.create(:user_details, :company, :user => @user)

# Error raised
Failure/Error: user_details << FactoryGirl.create(:user_details, :company, :user => @user)
NoMethodError:
undefined method `strip' for #
# ./app/models/user_details.rb:17:in `sanitize_data'
# ./spec/models/user_details_spec.rb:10:in `block (4 levels) in '
# ./spec/models/user_details_spec.rb:9:in `each'
# ./spec/models/user_details_spec.rb:9:in `block (3 levels) in '

# This is how factory is defined
FactoryGirl.define do
factory :user_details do
user { |u| u.association(:user) }

trait :company do
name "Company"
value "Ericsson"
end
end
end

# factory_girl entries in Gemlock:
factory_girl (2.5.0)
activesupport
factory_girl_rails (1.6.0)
factory_girl (~> 2.5.0)
railties (>= 3.0.0)