Reputation: 201
I made a mistake in the spelling of a model attribute name so I rolled back the changes and updated the model name to be the correct one. However, RSpec is still using the old model attribute name for some reason?
heres the spec:
....
let(:message_client) { FactoryGirl.attributes_for(:contact_message, client_id: alice.id) }
it "should redirect to the user edit page if a user is signed in" do
sign_in alice
post :create, contact_message: message_client
expect(response).to redirect_to edit_user_registration_path
end
Heres the FactoryGirl
FactoryGirl.define do
factory :contact_message do
contact_subject "Pay me"
contact_body "I didn't recieve my latest payment"
end
end
Heres the schema extract:
create_table "contact_messages", force: :cascade do |t|
t.string "contact_subject"
t.text "contact_body"
t.integer "freelancer_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
And here's the error:
Failure/Error: post :create, contact_message: message_client
NameError:
undefined local variable or method `contact_subject=' for #<ContactMessage:0x007fe9840b5e08>
And when I chuck a binding.pry in my controller create action and put in ContactMessage
this is what is returned:
3: def create
4: @message = current_freelancer.contact_messages.build(contact_message_params) if current_freelancer
=> 5: binding.pry
6: @message = current_user.contact_messages.build(contact_message_params) if current_user
7:
8: if @message.save
9: flash[:notice] = "Message Recieved"
10: redirect_to edit_freelancer_registration_path if current_freelancer
11: redirect_to edit_user_registration_path if current_user
12: else
13: redirect_to edit_freelancer_registration_path if current_freelancer
14: redirect_to edit_user_registration_path if current_user
15: end
16: end
[1] pry(#<ContactMessagesController>)> ContactMessage
=> ContactMessage(id: integer, messge_subject: string, message_body: text, freelancer_id: integer, user_id: integer, created_at: datetime, updated_at: datetime)
If you look down the bottom there it is still using the old fields for the model (messge_subject). Any ideas why it is doing this? I've tried restarting my comp and terminal to no avail.
Upvotes: 2
Views: 231
Reputation: 106802
It is a common oversight that RSpec uses another database than your development server.
If you have to rollback a migration you will have to rollback that migration in your test environment too:
RAILS_ENV=test bundle exec rake db:rollback
Upvotes: 4