Ayrton Senna
Ayrton Senna

Reputation: 3855

RSpec: All tests failing

I am following the Ruby on Rails tutorial by Michael Hartl. My tests were all passing till I migrated the database and added the last 2 test. Something got messed up since all the tests are failing now even if I remove the last 2 tests. I am not sure if I made some mistake with the migrate or something else.

This is what I used:

$ bundle exec rake db:migrate
$ bundle exec rake db:test:prepare
$ bundle exec rspec spec/

Here's the user_spec file which fails completely:

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 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[[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 "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
end

And here is the migration file:

class AddPassswordDigestToUsers < ActiveRecord::Migration
  def change
    add_column :users, :password_digest, :string
  end
end

EDIT2 - I just removed password: "foobar", password_confirmation: "foobar" from the first line of the spec and now I only get 5 errors (which is right as those related to password should fail)

EDIT1 - Here are the failing tests

Running all specs
Running tests with args ["--drb", "-f", "progress", "-r", "/home/niranjan/.rvm/gems/ruby-1.9.3-p194/gems/guard-rspec-1.2.1/lib/guard/rspec/formatters/notification_rspec.rb", "-f", "Guard::RSpec::Formatter::NotificationRSpec", "--out", "/dev/null", "--failure-exit-code", "2", "spec"]...
..FFFFFFFFFFFFFFF.........

Failures:

  1) User 
     Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", password: "foobar", password_confirmation: "foobar")
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:16:in `new'
     # ./spec/models/user_spec.rb:16:in `block (2 levels) in <top (required)>'

  2) User 
     Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", password: "foobar", password_confirmation: "foobar")
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:16:in `new'
     # ./spec/models/user_spec.rb:16:in `block (2 levels) in <top (required)>'

  3) User 
     Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", password: "foobar", password_confirmation: "foobar")
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:16:in `new'
     # ./spec/models/user_spec.rb:16:in `block (2 levels) in <top (required)>'

  4) User 
     Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", password: "foobar", password_confirmation: "foobar")
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:16:in `new'
     # ./spec/models/user_spec.rb:16:in `block (2 levels) in <top (required)>'

.... and so on

Upvotes: 0

Views: 1378

Answers (1)

Renato Zannon
Renato Zannon

Reputation: 30011

From the book:

Putting everything together gives the (failing) tests in Listing 6.28. We’ll get them to pass in Section 6.3.4.

The tests should be failing. They will pass as soon as the password implementation is done, later on the tutorial.


Now, to explain why they are failing:

On the line where you create the user (your before block), you are passing the password attribute to the constructor, but that attribute doesn't exist yet, so your model complains with the errors you are seeing. On the tutorial this attribute is created a little later.

Upvotes: 3

Related Questions