[英]rspec, unknown attribute question
我正在通過(優秀)railstutorial.org網站上有一個關於rspec的基本問題。
當我在新用戶模型上運行以下測試時,我收到“未知屬性:用戶名”消息和失敗的測試。
before(:each) do
@attr = { :lname_e => "User", :fname_e => "Test", :email => "user@example.com", :username => "testUser" }
end
it "should create a new instance given valid attributes" do
User.create!(@attr)
end
錯誤語法是
Failures:
1) User should create a new instance given valid attributes
Failure/Error: User.create!(@attr)
unknown attribute: username
# ./spec/models/user_spec.rb:11:in `block (2 levels) in <top (required)>'
該字段位於users表(字符串)中,它在模型中為attr_accessible,在控制台中,我可以在測試中創建具有完全相同語法的用戶。 創建初始表后,通過遷移添加了此“用戶名”字段,是否還需要在此處更新其他文件?
謝謝,
你有沒有運行rake db:test:prepare
?
測試數據庫中可能缺少該字段,但存在於開發數據庫中(這就是控制台工作的原因)。
嘗試確保您的遷移都是最新的,然后更新測試數據庫:
rake db:migrate
rake db:test:prepare
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.