我正在尝试在michael hartl教程的第10章中创建一个微博模型,我无法通过rspec测试。
以下是我所做的事情:
rails generate model Micropost content:string user_id:integer
rm -f spec/factories/microposts.rb
这是数据库迁移文件:
class CreateMicroposts < ActiveRecord::Migration
def change
create_table :microposts do |t|
t.string :content
t.integer :user_id
t.timestamps
end
add_index :microposts, [:user_id, :created_at]
end
end
这是微博的型号规范:
require 'spec_helper'
describe Micropost do
let(:user) { FactoryGirl.create(:user) }
before do
# This code is not idiomatically correct.
@micropost = Micropost.new(content: "Lorem ipsum", user_id: user.id)
end
subject { @micropost }
it { should respond_to(:content) }
it { should respond_to(:user_id) }
end
然后我做了:
bundle exec rake db:migrate
bundle exec rake test:prepare
我的错误消息是:
1) Micropost
Failure/Error: @micropost = Micropost.new(content: "Lorem ipsum", user_id: user.id)
ActiveRecord::StatementInvalid:
Could not find table 'microposts'
# ./spec/models/micropost_spec.rb:8:in `new'
# ./spec/models/micropost_spec.rb:8:in `block (2 levels) in <top (required)>'
2) Micropost
Failure/Error: @micropost = Micropost.new(content: "Lorem ipsum", user_id: user.id)
ActiveRecord::StatementInvalid:
Could not find table 'microposts'
# ./spec/models/micropost_spec.rb:8:in `new'
# ./spec/models/micropost_spec.rb:8:in `block (2 levels) in <top (required)>'
模式
ActiveRecord::Schema.define(:version => 20130801225814) do
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "password_digest"
t.string "remember_token"
t.boolean "admin", :default => false
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["remember_token"], :name => "index_users_on_remember_token"
end
我无法弄清楚问题,因为我非常有信心我完全按照第10章中的步骤进行操作。也许这是以前的事情?
感谢您的帮助!
答案 0 :(得分:1)
我认为它应该是bundle exec rake db:test:prepare
而不是bundle exec rake test:prepare