2

After successfully finishing the sign up page, with no failing tests, I get to 7.4.4 where he says to:

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

After I ran git checkout master, spork started complaining of 28 failures. After merging, it continued. Running bundle exec rspec spec/ I get 28 errors all having to do with the user_pages_spec.rb tests.

The errors are as follows:

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)>'

My user_pages_spec.rb file:

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

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

end

My models/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

What's the deal? Why is it failing? I don't understand.

1
  • I ran bundle exec rake db:migrate too, didn't do anything to change the failed tests. Oct 17, 2012 at 19:08

2 Answers 2

4

It seems you didn't run the migrations PGError: ERROR: relation "users" does not exist.

Try running bundle exec rake db:migrate

Let me know if that helps.

2
  • Run the migrations on test environment bundle exec RAILS_ENV=test rake db:migrate
    – Leo Correa
    Oct 17, 2012 at 19:10
  • 3
    Also, if its only failing on tests try running db:test:prepare and db:test:load.
    – Leo Correa
    Oct 17, 2012 at 19:12
1

Try running the migrations on the 'test' environment:

bundle exec rake db:migrate RAILS_ENV=test
2
  • db:test:prepare clones the development db to the test db. For this error however, I find it hard to believe that a simple migration wouldn't have sufficed...
    – PinnyM
    Oct 17, 2012 at 19:22
  • Yeah me too, I don't get how this happened. I think he skipped a step in the tutorial, or I did, or both. Oct 17, 2012 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.