title: RoR rspec failing test with error 'block (2 levels) in <top (required)>
body: <p>I am following Hartls Rails tutorial and I am stuck at the end of Chapter 6.
  Suddenly some of my test are failing with the same error:</p>
  
  <pre><code>C:\Users\JMA\Projekte\sampleapp&gt;bundle exec rspec spec/
  .F......F...FFF....
  
  Failures:
  
    1) User
       Failure/Error: it { should respond_to(:passwors_confirmation)}
         expected #&lt;User id: nil, name: "Example User", email: "user@example.com",
   created_at: nil, updated_at: nil, password_digest: "$2a$04$OMnlZgC9yWhxbQizs8MG
  9uPMCd8LmMlO5MZzkuQyM8it..."&gt; to respond to :passwors_confirmation
       # ./spec/models/user_spec.rb:16:in `block (2 levels) in &lt;top (required)&gt;'
  
    2) User when name is not present when email format is valid should be valid
       Failure/Error: expect(@user).to be_valid
         expected #&lt;User id: nil, name: " ", email: "user@foo.COM", created_at: ni
  l, updated_at: nil, password_digest: "$2a$04$PtGxi4DjJWFezLFYy1jW5eaI7C/xxyBLXAG
  zPepflo8r..."&gt; to be valid, but got errors: Name can't be blank
       # ./spec/models/user_spec.rb:51:in `block (5 levels) in &lt;top (required)&gt;'
       # ./spec/models/user_spec.rb:49:in `each'
       # ./spec/models/user_spec.rb:49:in `block (4 levels) in &lt;top (required)&gt;'
  
    3) User when name is not present return value of authenticate method with vali
  d password
       Failure/Error: before { @user.safe }
       NoMethodError:
         undefined method `safe' for #&lt;User:0x502bc30&gt;
       # ./spec/models/user_spec.rb:86:in `block (4 levels) in &lt;top (required)&gt;'
  
    4) User when name is not present return value of authenticate method with inva
  lid password
       Failure/Error: before { @user.safe }
       NoMethodError:
         undefined method `safe' for #&lt;User:0x50333a0&gt;
       # ./spec/models/user_spec.rb:86:in `block (4 levels) in &lt;top (required)&gt;'
  
    5) User when name is not present return value of authenticate method with inva
  lid password
       Failure/Error: before { @user.safe }
       NoMethodError:
         undefined method `safe' for #&lt;User:0x503b848&gt;
       # ./spec/models/user_spec.rb:86:in `block (4 levels) in &lt;top (required)&gt;'
  
  Finished in 0.52003 seconds
  19 examples, 5 failures
  
  Failed examples:
  
  rspec ./spec/models/user_spec.rb:16 # User
  rspec ./spec/models/user_spec.rb:47 # User when name is not present when email f
  ormat is valid should be valid
  rspec ./spec/models/user_spec.rb:90 # User when name is not present return value
   of authenticate method with valid password
  rspec ./spec/models/user_spec.rb:96 # User when name is not present return value
   of authenticate method with invalid password
  rspec ./spec/models/user_spec.rb:97 # User when name is not present return value
   of authenticate method with invalid password
  
  Randomized with seed 37132
  </code></pre>
  
  <p>My user_specs.rb looks like this:</p>
  
  <pre><code>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(:passwors_confirmation)}
  
    it { should be_valid }
  
  describe"when name is not present" do
      before { @user.name = " " }
      it { should_not be_valid}
  
  
  describe "when email is not present" do
      before { @user.email = " " }
      it { should_not be_valid}   
  end 
  
  describe "when name is to 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
          adresses = %w[user@foo,com user_at_foo.org example.user@foo. 
              foo@bar_baz.com foo@bar+baz.com]
          adresses.each do |invalid_adress|
              @user.email = invalid_adress
              expect(@user).not_to 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
          expect(@user).to be_valid
          end
      end
  end     
  
  describe "when email adress 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     
  
  it { should respond_to(:authenticate) }
  
  describe "when password is not present" do
      before do
      @user = User.new(name: "Example User", email: "user@example.com",
                         password: " ", password_confirmation: " ")
  end
  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 "with a password that's to short" do
      before { @user.password = @user.password_confirmation = "a" * 5 }
      it { should be_invalid }
  end
  
  describe "return value of authenticate method" do
      before { @user.safe }
      let(:found_user) { User.find_by(email: @user.email) }
  
      describe "with valid password" do
          it { should eq found_user.authenticate(@user.password) }
      end
  
      describe "with invalid password" do
          let (:user_for_inavlid_password) { found_user.authenticate("invalid") }
  
          it { should_not eq user_for_inavlid_password }
          specify { expect(user_for_inavlid_password).to be_false }
      end 
  end 
  
  end
  end
  </code></pre>
  
  <p>users.rb</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
  before_save { self.email = email.downcase }
  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence:true, format: { with: VALID_EMAIL_REGEX }, 
      uniqueness: { case_sensitive: false }
  
  has_secure_password     
  validates :password, length: { minimum: 6 }
  
  end
  </code></pre>
  
  <p>It appeared after adding the password and password_confirmation attributes to the User model. The where passing untill then. I have continued the tutorial becasue I have read about this failure and that it will be dealt with at a later stage of the tutorial. Now my tests are still failing and I do not know why. Any ideas?</p>