0

我第一次浏览 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
4

2 回答 2

6

当您运行测试时,使用的是测试数据库而不是开发。因此,每当您进行架构更改时,请确保您还运行rake db:test:prepare以在开发和测试数据库之间同步这些更改。

于 2012-07-03T03:36:17.173 回答
0

自OP以来已经有一段时间了,但我遇到了同样的问题。环境 Win7 64 位,带有 RailsInstaller。

跑步rake db:test:prepared并没有解决它。

跑步rake db:reset之后rake db:test:prepare终于做到了。

请注意,您必须在重置之前停止服务器。

于 2012-08-28T22:31:11.550 回答