RSpec使用模型的旧属性字段?

时间:2015-06-30 06:39:18

标签: ruby-on-rails ruby rspec

我在模型属性名称的拼写中犯了一个错误,因此我回滚了更改并将模型名称更新为正确的名称。但是,由于某种原因,RSpec仍在使用旧的模型属性名称?

继承人的规范:

....
  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

继承工厂女孩

FactoryGirl.define do
  factory :contact_message do
    contact_subject "Pay me"
    contact_body "I didn't recieve my latest payment"
  end
end

继承架构提取:

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

这是错误:

 Failure/Error: post :create, contact_message: message_client
 NameError:
   undefined local variable or method `contact_subject=' for #<ContactMessage:0x007fe9840b5e08>

当我在控制器中查找binding.pry时,创建操作并输入ContactMessage这是返回的内容:

     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)

如果你向下看那里,它仍然使用模型的旧字段(messge_subject)。任何想法为什么这样做?我试过重新启动我的comp和终端无济于事。

2 个答案:

答案 0 :(得分:4)

RSpec使用另一个数据库而不是开发服务器是一种常见的疏忽。

如果您必须回滚迁移,则还必须在测试环境中回滚该迁移:

RAILS_ENV=test bundle exec rake db:rollback

答案 1 :(得分:1)

要更新测试数据库,请尝试

rake db:test:prepare