2

成功完成注册页面后,没有失败的测试,我到达 7.4.4,他说:

$ git add .
$ git commit -m "Finish user signup"
$ git checkout master
$ git merge sign-up

在我运行 git checkout master 之后,spork 开始抱怨 28 次失败。合并后,继续。运行 bundle exec rspec spec/ 我得到 28 个与 user_pages_spec.rb 测试有关的错误。

错误如下:

Failures:

  1) User pages signup page 
     Failure/Error: before { visit signup_path }
     ActiveRecord::StatementInvalid:
       PGError: ERROR:  relation "users" does not exist
       LINE 4:              WHERE a.attrelid = '"users"'::regclass
                                               ^
       :             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
                     FROM pg_attribute a LEFT JOIN pg_attrdef d
                       ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                    WHERE a.attrelid = '"users"'::regclass
                      AND a.attnum > 0 AND NOT a.attisdropped
                    ORDER BY a.attnum
     # ./app/controllers/users_controller.rb:8:in `new'
     # ./app/controllers/users_controller.rb:8:in `new'
     # ./spec/requests/user_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

  2) User pages signup page 
     Failure/Error: before { visit signup_path }
     ActiveRecord::StatementInvalid:
       PGError: ERROR:  relation "users" does not exist
       LINE 4:              WHERE a.attrelid = '"users"'::regclass
                                               ^
       :             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
                     FROM pg_attribute a LEFT JOIN pg_attrdef d
                       ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                    WHERE a.attrelid = '"users"'::regclass
                      AND a.attnum > 0 AND NOT a.attisdropped
                    ORDER BY a.attnum
     # ./app/controllers/users_controller.rb:8:in `new'
     # ./app/controllers/users_controller.rb:8:in `new'
     # ./spec/requests/user_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

  3) User pages profile page 
     Failure/Error: let(:user) { FactoryGirl.create(:user) }
     ActiveRecord::StatementInvalid:
       PGError: ERROR:  relation "users" does not exist
       LINE 4:              WHERE a.attrelid = '"users"'::regclass
                                               ^
       :             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
                     FROM pg_attribute a LEFT JOIN pg_attrdef d
                       ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                    WHERE a.attrelid = '"users"'::regclass
                      AND a.attnum > 0 AND NOT a.attisdropped
                    ORDER BY a.attnum
     # ./spec/requests/user_pages_spec.rb:15:in `block (3 levels) in <top (required)>'
     # ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>'

  4) User pages profile page 
     Failure/Error: let(:user) { FactoryGirl.create(:user) }
     ActiveRecord::StatementInvalid:
       PGError: ERROR:  relation "users" does not exist
       LINE 4:              WHERE a.attrelid = '"users"'::regclass
                                               ^
       :             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
                     FROM pg_attribute a LEFT JOIN pg_attrdef d
                       ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                    WHERE a.attrelid = '"users"'::regclass
                      AND a.attnum > 0 AND NOT a.attisdropped
                    ORDER BY a.attnum
     # ./spec/requests/user_pages_spec.rb:15:in `block (3 levels) in <top (required)>'
     # ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>'

  5) User pages signup with invalid information should not create a user
     Failure/Error: before { visit signup_path }
     ActiveRecord::StatementInvalid:
       PGError: ERROR:  relation "users" does not exist
       LINE 4:              WHERE a.attrelid = '"users"'::regclass
                                               ^
       :             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
                     FROM pg_attribute a LEFT JOIN pg_attrdef d
                       ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                    WHERE a.attrelid = '"users"'::regclass
                      AND a.attnum > 0 AND NOT a.attisdropped
                    ORDER BY a.attnum
     # ./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)>'

  6) User pages signup with valid information should create a user
     Failure/Error: before { visit signup_path }
     ActiveRecord::StatementInvalid:
       PGError: ERROR:  relation "users" does not exist
       LINE 4:              WHERE a.attrelid = '"users"'::regclass
                                               ^
       :             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
                     FROM pg_attribute a LEFT JOIN pg_attrdef d
                       ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                    WHERE a.attrelid = '"users"'::regclass
                      AND a.attnum > 0 AND NOT a.attisdropped
                    ORDER BY a.attnum
     # ./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)>'

我的 user_pages_spec.rb 文件:

require 'spec_helper'

describe "User pages" do

  subject { page }

  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 "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" do

    before { visit signup_path }

    let(:submit) { "Create my account" }

    describe "with invalid information" do
      it "should not create a user" do
        expect { click_button submit }.not_to change(User, :count)
      end
    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 { click_button submit }.to change(User, :count).by(1)
      end
    end
  end

end

我的用户控制器:

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
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

end

我的模型/user_spec.rb

require 'spec_helper'

describe User do

  before do
    @user = User.new(name: "Example User", email: "user@example.com", password: "foobar", password_confirmation: "foobar")
  end

  subject { @user }

  it { should respond_to(:name) }
  it { should respond_to(:email) }
  it { should respond_to(:password_digest) }
  it { should respond_to(:password) }
  it { should respond_to(:password_confirmation) }
  it { should respond_to(:authenticate) }

  it { should be_valid }

  describe "when name is not present" do
    before { @user.name = " " }
    it { should_not be_valid }
  end

  describe "when email is not present" do
    before { @user.email = " " }
    it { should_not be_valid }
  end

  describe "when name is too long" do
    before { @user.name = "a" * 51 }
    it { should_not be_valid }
  end  

  describe "when email format is invalid" do
    it "should be invalid" do
        addresses = %w[user@foo,com user_at_foo.org example.user@foo.foo@bar_baz.com foo@bar+baz.com]
        addresses.each do | invalid_address|
            @user.email = invalid_address
            @user.should_not be_valid
        end
    end
  end

  describe "when email format is valid" do
    it "should be valid" do
        addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
        addresses.each do |valid_address|
            @user.email = valid_address
            @user.should be_valid
        end
    end
  end

  describe "when email address is already taken" do
    before do
      user_with_same_email = @user.dup
      user_with_same_email.email = @user.email.upcase
      user_with_same_email.save
    end

    it { should_not be_valid }
  end

  describe "email address with mixed case" do
    let(:mixed_case_email) { "Foo@ExAMPle.CoM"}

    it "should be saved as all lower-case" do
      @user.email = mixed_case_email
      @user.save
      @user.reload.email.should == mixed_case_email.downcase
    end
  end

  describe "when password is not present" do
    before { @user.password = @user.password_confirmation = " " }
    it { should_not be_valid }
  end

  describe "when password doesn't match confirmation" do
    before { @user.password_confirmation = "mismatch" }
    it { should_not be_valid }
  end

  describe "when password confirmation is nil" do
    before { @user.password_confirmation = nil }
    it { should_not be_valid }
  end

  describe "return value of authenticate method" do
    before { @user.save }
    let(:found_user) { User.find_by_email(@user.email) }

    describe "with valid password" do
      it { should == found_user.authenticate(@user.password) }
    end

    describe "with invalid password" do
      let(:user_for_invalid_password) { found_user.authenticate("invalid") }

      it { should_not == user_for_invalid_password }
      specify { user_for_invalid_password.should be_false }
    end
  end

  describe "with a password that's too short" do
    before { @user.password = @user.password_confirmation = "a" * 5 }
    it { should be_invalid }
  end

end

这是怎么回事?为什么会失败?我不明白。

4

2 回答 2

4

看来您没有运行 migrations PGError: ERROR: relation "users" does not exist

尝试运行bundle exec rake db:migrate

让我知道这是否有帮助。

于 2012-10-17T19:08:14.393 回答
1

尝试在“测试”环境中运行迁移:

bundle exec rake db:migrate RAILS_ENV=test
于 2012-10-17T19:09:46.607 回答