RoR教程Hartl:第9.1章中的失败测试 - ActiveRecord

时间:2012-07-03 03:02:44

标签: ruby-on-rails

我第一次浏览Michael Hartl的教程并且无法完成第9章的第一次测试(Section 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中有一个用户表:

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

database.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

2 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:0)

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

正在运行rake db:test:prepared并没有解决问题。

最后运行rake db:reset后跟rake db:test:prepare

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