Учебное пособие по Rails Глава 7.4.4 Все тесты не проходят после Git

После успешного завершения страницы регистрации, без неудачных тестов, я добираюсь до 7.4.4, где он говорит:

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

После того, как я запустил git checkout master, spork начал жаловаться на 28 сбоев. После слияния это продолжилось. При запуске пакета 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: "[email protected]"
        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: "[email protected]", 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 [email protected]@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[[email protected] [email protected] [email protected] [email protected]]
        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) { "[email protected]"}

    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

В чем дело? Почему это не удается? Я не понимаю.


person Nathan McKaskle    schedule 17.10.2012    source источник
comment
Я также запускал пакет exec rake db:migrate, ничего не делал, чтобы изменить неудачные тесты.   -  person Nathan McKaskle    schedule 17.10.2012


Ответы (2)


Кажется, вы не выполнили миграцию PGError: ERROR: relation "users" does not exist.

Попробуйте запустить bundle exec rake db:migrate

Дайте мне знать, если это поможет.

person Leo Correa    schedule 17.10.2012
comment
Запустите миграцию в пакете тестовой среды exec RAILS_ENV=test rake db:migrate - person Leo Correa; 17.10.2012
comment
Кроме того, если он не работает только в тестах, попробуйте запустить db:test:prepare и db:test:load. - person Leo Correa; 17.10.2012

Попробуйте запустить миграцию в тестовой среде:

bundle exec rake db:migrate RAILS_ENV=test
person PinnyM    schedule 17.10.2012
comment
db:test:prepare клонирует базу данных разработки в тестовую базу данных. Однако для этой ошибки мне трудно поверить, что простой миграции было бы недостаточно... - person PinnyM; 17.10.2012
comment
Да я тоже, я не понимаю, как это произошло. Я думаю, что он пропустил шаг в учебнике, или я пропустил, или и то, и другое. - person Nathan McKaskle; 17.10.2012