mercredi 11 mars 2015

Rails ActiveRecord Model not updating in RSpec model spec

This is driving me insane. I've stripped this down to the bare minimum without losing context (I think!)


All I am trying to do is check that when I update a value and save it to the database, that the value was saved. I want to do this because I need to write some other code that conditionally prevents this in the before_save callback, and I can't test that until I'm sure this is working!


The factories and the spec are below, I'm sure its something really stupid but I just can't figure it out...



FactoryGirl.define do

factory :programme do
name 'Trainee Programme'
end

factory :membership do
programme
end

factory :specialty do
sequence(:name) { |n| "Specialty #{n}" }
end

factory :user do
sequence(:email) { |n| "factorygirl-user-#{n}@remailer.org" }
password 'password'
password_confirmation 'password'
factory :trainee, class: User do
sequence(:email) { |n| "factorygirl-trainee-#{n}@remailer.org" }
name 'Factory Girl Trainee'
after(:create) do |user|
FactoryGirl.create(:membership, user: user, start_date: 1.day.ago)
end
end
end

end

describe Membership do

let(:trainee) { FactoryGirl.create(:trainee) }

it 'sets specialty' do
puts trainee.current_membership.inspect
trainee.current_membership.specialty = specialty
puts trainee.current_membership.inspect
trainee.current_membership.save!
puts trainee.current_membership.inspect
expect(trainee.current_membership.specialty).to eq(specialty)
end

end


The spec is failing because the expect sees a nil value. When I run the code the debug output I get is:



#<Membership id: 11, user_id: 11, programme_id: 11, start_date: "2015-03-10", end_date: nil, created_at: "2015-03-11 22:02:51", updated_at: "2015-03-11 22:02:51", options: {}, specialty_id: nil, membership_type_id: nil>
#<Membership id: 11, user_id: 11, programme_id: 11, start_date: "2015-03-10", end_date: nil, created_at: "2015-03-11 22:02:51", updated_at: "2015-03-11 22:02:51", options: {}, specialty_id: nil, membership_type_id: nil>
#<Membership id: 11, user_id: 11, programme_id: 11, start_date: "2015-03-10", end_date: nil, created_at: "2015-03-11 22:02:51", updated_at: "2015-03-11 22:02:51", options: {}, specialty_id: nil, membership_type_id: nil>


So its as if the assignment of specialty never happens??


Aucun commentaire:

Enregistrer un commentaire