我正在尝试在 michael hartl 教程的第 10 章中创建一个 micropost 模型,但我无法通过 rspec 测试。
这是我所做的:
rails generate model Micropost content:string user_id:integer
rm -f spec/factories/microposts.rb
这是 db 迁移文件:
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
这是 micropost 的模型规格:
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 章中的步骤进行操作。也许是以前的东西?
谢谢你的帮助!