title: Rails Tutorial 3 Chapter 7: User Model Rspec Test Failing
body: <p>I've been following the Rails Tutorial 3 successfully until I got to chapter 7 and implemented the user model, now my rspec keeps failing. </p>
  
  <p>Here's my user.rb file output</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
  attr_accessible :name, :email
  
  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  
  validates :name,     :presence =&gt; true,
                       :length   =&gt; { :maximum =&gt; 50 }
  
  validates :email,    :presence   =&gt; true,
                       :format     =&gt; { :with =&gt; email_regex },
                       :uniqueness =&gt; { :case_sensitive =&gt; false }
  
  validates :password, :presence =&gt; true,
                       :confirmation =&gt; true,
                       :length =&gt; { :within =&gt; 6..40 }
  
  before_save :encrypt_password
  
  # Return tue if the user's password matches the submitted password.
      def has_password?(submitted_password)
          encrypted_password == encrypt(submitted_password)
      end
  
      def self.authenticate(email, submitted_password)
          user = find_by_email(email)
          return nil  if user.nil?
          return user if user.has_password?(submitted_password)
      end
  
  private
  
  def encrypt_password
    self.salt = make_salt unless has_password?(password)
    self.encrypted_password = encrypt(password)
  end
  
  def encrypt(string)
    secure_hash("#{salt}--#{string}")
  end
  
  def make_salt
    secure_hash("#{Time.now.utc}--#{password}")
  end
  
  def secure_hash(string)
    Digest::SHA2.hexdigest(string)
  end
  end
  </code></pre>
  
  <p>Here's my users_spec.rb</p>
  
  <pre><code>require 'spec_helper'
  
  describe User do
  
    before(:each) do
    @attr = { 
    :name =&gt; "Example User",
    :email =&gt; "user@example.com",
    :password =&gt; "foobar",
    :password_confirmation =&gt; "foobar"
      }
    end
  
    it "should create a new instance given a valid attribute" do
      User.create!(@attr)
    end
  
    it "should require a name" do
      no_name_user = User.new(@attr.merge(:name =&gt; ""))
      no_name_user.should_not be_valid
    end
  
    it "should require an email address" do
      no_email_user = User.new(@attr.merge(:email =&gt; ""))
      no_email_user.should_not be_valid
    end
  
    it "should reject names that are too long" do
      long_name = "a" * 51
      long_name_user = User.new(@attr.merge(:name =&gt; long_name))
      long_name_user.should_not be_valid
    end
  
    it "should accept valid email addresses" do
      addresses = %w[user@foo.com THE_USER@foo.bar.org first.last@foo.jp]
      addresses.each do |address|
        valid_email_user = User.new(@attr.merge(:email =&gt; address))
        valid_email_user.should be_valid
      end
    end
  
    it "should reject invalid email addresses" do
      addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
      addresses.each do |address|
        invalid_email_user = User.new(@attr.merge(:email =&gt; address))
        invalid_email_user.should_not be_valid
      end
     end
  
    it "should reject duplicate email addresses" do
      User.create!(@attr)
      user_with_duplicate_email = User.new(@attr)
      user_with_duplicate_email.should_not be_valid
    end
  
    it "should reject email addresses identical up to case" do
      upcased_email = @attr[:email].upcase
      User.create!(@attr.merge(:email =&gt; upcased_email))
      user_with_duplicate_email = User.new(@attr)
      user_with_duplicate_email.should_not be_valid
    end
  
    describe "passwords" do
  
  before(:each) do
        @user = User.create!(@attr)
      end
  
      it "should have a password attribute" do
        @user.should respond_to(:password)
      end
  
  it "should have a password confirmation attribute" do
    @user.should respond_to(:password_confirmation)
  end
    end
  
        describe "password validations" do
  
      it "should require a password" do
        User.new(@attr.merge(:password =&gt; "", :password_confirmation =&gt; "")).
          should_not be_valid
      end
  
      it "should require a matching password confirmation" do
        User.new(@attr.merge(:password_confirmation =&gt; "invalid")).
          should_not be_valid
      end
  
      it "should reject short passwords" do
        short = "a" * 5
        hash = @attr.merge(:password =&gt; short, :password_confirmation =&gt; short)
        User.new(hash).should_not be_valid
      end
  
      it "should reject long passwords" do
        long = "a" * 41
        hash = @attr.merge(:password =&gt; long, :password_confirmation =&gt; long)
        User.new(hash).should_not be_valid
      end
    end
  end
  </code></pre>
  
  <p>Finally here's the output of my rspec</p>
  
  <pre><code>Failures:
  
    1) UsersController GET 'show' should be successfull
   Failure/Error: @user = Factory(:user)
   ArgumentError:
     Factory not registered: user
   # ./spec/controllers/users_controller_spec.rb:9:in `block (3 levels) in &lt;top (required)&gt;'
  
    2) UsersController GET 'show' should find the right user
   Failure/Error: @user = Factory(:user)
   ArgumentError:
     Factory not registered: user
   # ./spec/controllers/users_controller_spec.rb:9:in `block (3 levels) in &lt;top (required)&gt;'
  
    3) User should create a new instance given a valid attribute
   Failure/Error: User.create!(@attr)
   NoMethodError:
     undefined method `password' for #&lt;User:0x007f9d3684e0b0&gt;
   # ./spec/models/user_spec.rb:15:in `block (2 levels) in &lt;top (required)&gt;'
  
    4) User should require a name
   Failure/Error: no_name_user.should_not be_valid
   NoMethodError:
     undefined method `password' for #&lt;User:0x007f9d36eacf38&gt;
   # ./spec/models/user_spec.rb:20:in `block (2 levels) in &lt;top (required)&gt;'
  
    5) User should require an email address
   Failure/Error: no_email_user.should_not be_valid
   NoMethodError:
     undefined method `password' for #&lt;User:0x007f9d36e45978&gt;
   # ./spec/models/user_spec.rb:25:in `block (2 levels) in &lt;top (required)&gt;'
  
    6) User should reject names that are too long
   Failure/Error: long_name_user.should_not be_valid
   NoMethodError:
     undefined method `password' for #&lt;User:0x007f9d36e0b2a0&gt;
   # ./spec/models/user_spec.rb:31:in `block (2 levels) in &lt;top (required)&gt;'
  
    7) User should accept valid email addresses
   Failure/Error: valid_email_user.should be_valid
   NoMethodError:
     undefined method `password' for #&lt;User:0x007f9d36da4c80&gt;
   # ./spec/models/user_spec.rb:38:in `block (3 levels) in &lt;top (required)&gt;'
   # ./spec/models/user_spec.rb:36:in `each'
   # ./spec/models/user_spec.rb:36:in `block (2 levels) in &lt;top (required)&gt;'
  
    8) User should reject invalid email addresses
   Failure/Error: invalid_email_user.should_not be_valid
   NoMethodError:
     undefined method `password' for #&lt;User:0x007f9d36d870b8&gt;
   # ./spec/models/user_spec.rb:46:in `block (3 levels) in &lt;top (required)&gt;'
   # ./spec/models/user_spec.rb:44:in `each'
   # ./spec/models/user_spec.rb:44:in `block (2 levels) in &lt;top (required)&gt;'
  
    9) User should reject duplicate email addresses
   Failure/Error: User.create!(@attr)
   NoMethodError:
     undefined method `password' for #&lt;User:0x007f9d36c6c890&gt;
   # ./spec/models/user_spec.rb:51:in `block (2 levels) in &lt;top (required)&gt;'
  
    10) User should reject email addresses identical up to case
   Failure/Error: User.create!(@attr.merge(:email =&gt; upcased_email))
   NoMethodError:
     undefined method `password' for #&lt;User:0x007f9d36c4d878&gt;
   # ./spec/models/user_spec.rb:58:in `block (2 levels) in &lt;top (required)&gt;'
  
    11) User passwords should have a password attribute
   Failure/Error: @user = User.create!(@attr)
   NoMethodError:
     undefined method `password' for #&lt;User:0x007f9d36b3cda8&gt;
   # ./spec/models/user_spec.rb:66:in `block (3 levels) in &lt;top (required)&gt;'
  
    12) User passwords should have a password confirmation attribute
   Failure/Error: @user = User.create!(@attr)
   NoMethodError:
     undefined method `password' for #&lt;User:0x007f9d369c27c0&gt;
   # ./spec/models/user_spec.rb:66:in `block (3 levels) in &lt;top (required)&gt;'
  
    13) User password validations should require a password
   Failure/Error: User.new(@attr.merge(:password =&gt; "", :password_confirmation =&gt; "")).
   NoMethodError:
     undefined method `password' for #&lt;User:0x007f9d3699e5f0&gt;
   # ./spec/models/user_spec.rb:81:in `block (3 levels) in &lt;top (required)&gt;'
  
    14) User password validations should require a matching password confirmation
   Failure/Error: User.new(@attr.merge(:password_confirmation =&gt; "invalid")).
   NoMethodError:
     undefined method `password' for #&lt;User:0x007f9d3698e600&gt;
   # ./spec/models/user_spec.rb:86:in `block (3 levels) in &lt;top (required)&gt;'
  
    15) User password validations should reject short passwords
   Failure/Error: User.new(hash).should_not be_valid
   NoMethodError:
     undefined method `password' for #&lt;User:0x007f9d3697dda0&gt;
   # ./spec/models/user_spec.rb:93:in `block (3 levels) in &lt;top (required)&gt;'
  
    16) User password validations should reject long passwords
   Failure/Error: User.new(hash).should_not be_valid
   NoMethodError:
     undefined method `password' for #&lt;User:0x007f9d3696c5a0&gt;
   # ./spec/models/user_spec.rb:99:in `block (3 levels) in &lt;top (required)&gt;'
  
  Finished in 0.80301 seconds
  35 examples, 16 failures, 2 pending
  
  Failed examples:
  
  rspec ./spec/controllers/users_controller_spec.rb:12 # UsersController GET 'show' should be successfull
  rspec ./spec/controllers/users_controller_spec.rb:17 # UsersController GET 'show' should find the right user
  rspec ./spec/models/user_spec.rb:14 # User should create a new instance given a valid attribute
  rspec ./spec/models/user_spec.rb:18 # User should require a name
  rspec ./spec/models/user_spec.rb:23 # User should require an email address
  rspec ./spec/models/user_spec.rb:28 # User should reject names that are too long
  rspec ./spec/models/user_spec.rb:34 # User should accept valid email addresses
  rspec ./spec/models/user_spec.rb:42 # User should reject invalid email addresses
  rspec ./spec/models/user_spec.rb:50 # User should reject duplicate email addresses
  rspec ./spec/models/user_spec.rb:56 # User should reject email addresses identical up to case
  rspec ./spec/models/user_spec.rb:69 # User passwords should have a password attribute
  rspec ./spec/models/user_spec.rb:73 # User passwords should have a password confirmation attribute
  rspec ./spec/models/user_spec.rb:80 # User password validations should require a password
  rspec ./spec/models/user_spec.rb:85 # User password validations should require a matching password confirmation
  rspec ./spec/models/user_spec.rb:90 # User password validations should reject short passwords
  rspec ./spec/models/user_spec.rb:96 # User password validations should reject long passwords
  </code></pre>
  
  <p>Any ideas on what's going on? I've been stuck on this for about a week now</p>