我第一次浏览 Michael Hartl 的教程,并且无法通过第 9 章的第一次测试(第 9.1.1 节通过。我收到了引用 ActiveRecord 问题的返回,但我检查了我的架构和参考代码而且我很难找到我的问题。以前有人因为 users_controller 中缺少一行而遇到过这个问题,但我仔细检查过,这似乎不是我的问题,所以任何帮助都会很大赞赏。
这是我得到的回报:
$ bundle exec rspec spec/requests/user_pages_spec.rb -e "edit page"
Run options: include {:full_description=>/(?-mix:edit\ page)/}
FFF
Failures:
1) User pages signup edit page
Failure/Error: before { visit signup_path }
ActiveRecord::StatementInvalid:
Could not find table 'users'
# ./app/controllers/users_controller.rb:8:in `new'
# ./app/controllers/users_controller.rb:8:in `new'
# ./spec/requests/user_pages_spec.rb:24:in `block (3 levels) in <top (required)>'
2) User pages signup edit page
Failure/Error: before { visit signup_path }
ActiveRecord::StatementInvalid:
Could not find table 'users'
# ./app/controllers/users_controller.rb:8:in `new'
# ./app/controllers/users_controller.rb:8:in `new'
# ./spec/requests/user_pages_spec.rb:24:in `block (3 levels) in <top (required)>'
3) User pages signup edit page
Failure/Error: before { visit signup_path }
ActiveRecord::StatementInvalid:
Could not find table 'users'
# ./app/controllers/users_controller.rb:8:in `new'
# ./app/controllers/users_controller.rb:8:in `new'
# ./spec/requests/user_pages_spec.rb:24:in `block (3 levels) in <top (required)>'
Finished in 5.45 seconds
3 examples, 3 failures
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:64 # User pages signup edit page
rspec ./spec/requests/user_pages_spec.rb:65 # User pages signup edit page
rspec ./spec/requests/user_pages_spec.rb:66 # User pages signup edit page
还有我的代码——user_pages_spec.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }
end
describe "signup page" do
before { visit signup_path }
it { should have_selector('h1', text: 'Sign up') }
it { should have_selector('title', text: full_title('Sign up')) }
end
describe "signup" do
before { visit signup_path }
describe "with invalid information" do
it "should not create a user" do
expect { click_button "Create my account"}.not_to change(User, :count)
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "user@example.com"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end
it "should create a user" do
expect do
click_button "Create my account"
end.to change(User, :count).by(1)
end
describe "after saving the user" do
before { click_button "Create my account" }
let(:user) { User.find_by_email('user@example.com') }
it { should have_selector('title', text: user.name) }
it { should have_selector('div.alert.alert-success', text: 'Welcome') }
it { should have_link('Sign out') }
end
end
end
describe "edit" do
let(:user) { FactoryGirl.create(:user) }
before do
sign_in user
visit edit_user_path(user)
end
describe "page" do
it { should have_selector('h1', text: "Update your profile") }
it { should have_selector('title', text: "Edit user") }
it { should have_link('change', href: 'http://gravatar.com/emails') }
end
describe "with invalid information" do
before { click_button "Save changes" }
it { should have_content('error') }
end
end
end
end
我确认我的 schema.rb 中有一个 users 表:
ActiveRecord::Schema.define(:version => 20120625023223) 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"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["remember_token"], :name => "index_users_on_remember_token"
end
还有我的 users_controller:
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
def edit
@user = User.find(params[:id])
end
end
数据库.yml:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000