简体   繁体   中英

Failure/Error:before { visit user_path(user) }

I am currently at chapter 7 of michael hartl's tutorial and i keep getting failing test suites.

When i run bundle exec rspec spec/requests/user_pages_spec.rb i get the following failures. Can anyone shed some light on this??

Failures:                                                                                                                            

  1) User pages profile page                                                                                                         
     Failure/Error: before { visit user_path(user) }                                                                                 
     ActionView::Template::Error:                                                                                                    
       undefined method `digest' for #<Class:0x007fadc6123b28>                                                                       
     # ./app/helpers/sessions_helper.rb:19:in `current_user'                                                                         
     # ./app/helpers/sessions_helper.rb:11:in `signed_in?'                                                                           
     # ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__873134851071575186_70192169771720'            
     # ./app/views/layouts/application.html.erb:12:in `_app_views_layouts_application_html_erb__3681928384030501247_70192144555120'  
     # ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'                                                

  2) User pages profile page                                                                                                         
     Failure/Error: before { visit user_path(user) }                                                                                 
     ActionView::Template::Error:                                                                                                    
       undefined method `digest' for #<Class:0x007fadc6123b28>                                                                       
     # ./app/helpers/sessions_helper.rb:19:in `current_user'                                                                         
     # ./app/helpers/sessions_helper.rb:11:in `signed_in?'                                                                           
     # ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__873134851071575186_70192169771720'            
     # ./app/views/layouts/application.html.erb:12:in `_app_views_layouts_application_html_erb__3681928384030501247_70192144555120'  
     # ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'                                                

  3) User pages signup page                                                                                                          
     Failure/Error: before { visit signup_path }                                                                                     
     ActionView::Template::Error:                                                                                                    
       First argument in form cannot contain nil or be empty                                                                         
     # ./app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb___2899461793876151964_70192172593300'                      
     # ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>'                                               

  4) User pages signup page                                                                                                          
     Failure/Error: before { visit signup_path }                                                                                     
     ActionView::Template::Error:                                                                                                    
       First argument in form cannot contain nil or be empty                                                                         
     # ./app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb___2899461793876151964_70192172593300'                      
     # ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>'

The following is my user_spec.rb:

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(: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 example.user@foo.
                     foo@bar_baz.com foo@bar+baz.com]
      addresses.each do |invalid_address|
        @user.email = invalid_address
        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 address is already taken" do
    before do
      user_with_same_email = @user.dup
      user_with_same_email.save
    end

    it { should_not be_valid }
  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 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 too short" do
    before { @user.password = @user.password_confirmation = "a" * 5 }
    it { should be_invalid }
  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 eq found_user.authenticate(@user.password) }
    end

    describe "with invalid password" do
      let(:user_for_invalid_password) { found_user.authenticate("invalid") }

      it { should_not eq user_for_invalid_password }
      specify { expect(user_for_invalid_password).to be_false }
    end
  end
end

And this is my user_pages_spec.rb:

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit user_path(user) }

    it { should have_content(user.name) }
    it { should have_title(user.name) }
  end

  describe "signup page" do
    before { visit signup_path }

    it { should have_content('Sign up') }
    it { should have_title(full_title('Sign up')) }
  end
end

The following is users_helper.rb:

module UsersHelper

  # Returns the Gravatar (http://gravatar.com/) for the given user.
  def gravatar_for(user)
    gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
    gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
    image_tag(gravatar_url, alt: user.name, class: "gravatar")
  end
end

Can anyone shed some light on this?

While you mentioned that you are currently on Chapter 7 of the book, the tests failures that are occurring appear in Chapter 8 . Perhaps you should check whether the state of your code base matches where you currently are in the book.

For example, the reference to User.digest in app/helpers/sessions_helper.rb .

Failures:

1) User pages profile page
Failure/Error: before { visit user_path(user) }
ActionView::Template::Error:
undefined method digest' for #<Class:0x007fadc6123b28>
# ./app/helpers/sessions_helper.rb:19:in
digest' for #<Class:0x007fadc6123b28>
# ./app/helpers/sessions_helper.rb:19:in
digest' for #<Class:0x007fadc6123b28>
# ./app/helpers/sessions_helper.rb:19:in
current_user'

# ./app/helpers/sessions_helper.rb:11:in signed_in?'
# ./app/views/layouts/_header.html.erb:9:in
signed_in?'
# ./app/views/layouts/_header.html.erb:9:in
signed_in?'
# ./app/views/layouts/_header.html.erb:9:in
_app_views_layouts__header_html_erb__873134851071575186_70192169771720'

# ./app/views/layouts/application.html.erb:12:in _app_views_layouts_application_html_erb__3681928384030501247_70192144555120'
# ./spec/requests/user_pages_spec.rb:9:in
_app_views_layouts_application_html_erb__3681928384030501247_70192144555120'
# ./spec/requests/user_pages_spec.rb:9:in
_app_views_layouts_application_html_erb__3681928384030501247_70192144555120'
# ./spec/requests/user_pages_spec.rb:9:in
block (3 levels) in '

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM