Hi All,
I realize I have a similar question to Gordon.
When I have an association in a factory, attributes_for does not provide a value for that association. So I can not use it out of the box.
My hack has been to merge a previously created model, which adds the association attributes. (i.e: zombie_type and zombie_color)
Can you suggest an alternate way to solve this problem?
Due to uniqueness constraints, I can not use pass @zombie.attributes.
Thanks,
Keenan
class Zombie < ActiveRecord::Base
belongs_to :zombie_type
belongs_to :zombie_color
validates :image, :zombie_type, :color_name, :presence => true
validates :name, :presence => true, :uniqueness => true
#snip
end
FactoryGirl.define do
factory :zombie do
sequence(:name) { |n| "zombie_#{n}"}
image 'image.jpg'
association :zombie_type
association :zombie_color
end
end
class ZombiesControllerTest < ActionController::TestCase
setup do
@zombie = FactoryGirl.create(:zombie)
end
#snip
test "should create zombie" do
assert_difference('Zombie.count') do
post :create, zombie: attributes_with_associations
assert assigns(:zombie).valid?, assigns(:zombie).errors.full_messages.join(", ")
assert_match /success/, notice
end
assert_redirected_to zombies_path
end
#snip
private
# LOOK HERE
def attributes_with_associations
@zombie.attributes.merge(FactoryGirl.attributes_for(:zombie))
end
#/LOOK HERE
def notice
flash[:notice]
end
end