繁体   English   中英

Hartl Ruby on Rails教程-第8章测试失败

[英]Hartl Ruby on Rails Tutorial - Chapter 8 failed test

我目前正在研究Michael Hartl的Ruby on Rails教程。 在本章中,我得到了4个单独的错误,非常感谢您为解决这些错误提供的帮助。

$ bundle exec rake测试

   1) Error:
SiteLayoutTest#test_layout_links:
NoMethodError: undefined method `full_title' for #<SiteLayoutTest:0x000000049fd568>
    test/integration/site_layout_test.rb:13:in `block in <class:SiteLayoutTest>'


  2) Error:
UsersLoginTest#test_login_with_remembering:
ActionController::RoutingError: No route matches [POST] "/login"
    test/test_helper.rb:18:in `log_in_as'
    test/integration/users_login_test.rb:31:in `block in <class:UsersLoginTest>'


  3) Error:
UsersLoginTest#test_login_with_valid_information_followed_by_logout:
ActionController::RoutingError: No route matches [POST] "/login"
    test/integration/users_login_test.rb:11:in `block in <class:UsersLoginTest>'


  4) Error:
UsersLoginTest#test_login_without_remembering:
ActionController::RoutingError: No route matches [POST] "/login"
    test/test_helper.rb:18:in `log_in_as'
    test/integration/users_login_test.rb:36:in `block in <class:UsersLoginTest>'

23 runs, 39 assertions, 0 failures, 4 errors, 0 skips

$ bundle exec耙路线

   Prefix Verb   URI Pattern               Controller#Action
     root GET    /                         static_pages#home
     help GET    /help(.:format)           static_pages#help
    about GET    /about(.:format)          static_pages#about
  contact GET    /contact(.:format)        static_pages#contact
   signup GET    /signup(.:format)         users#new
    login GET    /login(.:format)          sessions#new
          GET    /login(.:format)          sessions#create
   logout GET    /logout(.:format)         sessions#destroy
    users GET    /users(.:format)          users#index
          POST   /users(.:format)          users#create
 new_user GET    /users/new(.:format)      users#new
edit_user GET    /users/:id/edit(.:format) users#edit
     user GET    /users/:id(.:format)      users#show
          PATCH  /users/:id(.:format)      users#update
          PUT    /users/:id(.:format)      users#update
          DELETE /users/:id(.:format)      users#destroy

配置/ routes.rb中

Rails.application.routes.draw do

  root             'static_pages#home'
  get 'help'    => 'static_pages#help'
  get 'about'   => 'static_pages#about'
  get 'contact' => 'static_pages#contact'
  get 'signup'  => 'users#new'
  get 'login'   => 'sessions#new'
  get 'login'   => 'sessions#create'
  get 'logout'  => 'sessions#destroy'
  resources :users
end

测试/集成/ users_login_test.rb

require 'test_helper'

class UsersLoginTest < ActionDispatch::IntegrationTest

  def setup
    @user = users(:michael)
  end

  test "login with valid information followed by logout" do
    get login_path
    post login_path, session: { email: @user.email, password: 'password' }
    assert is_logged_in?
    assert_redirected_to @user
    follow_redirect!
    assert_template 'users/show'
    assert_select "a[href=?]", login_path, count: 0
    assert_select "a[href=?]", logout_path
    assert_select "a[href=?]", user_path(@user)
    delete logout_path
    assert_not is_logged_in?
    assert_redirected_to root_url
    # Simulate a user clicking logout in a second window.
    delete logout_path
    follow_redirect!
    assert_select "a[href=?]", login_path
    assert_select "a[href=?]", logout_path,      count: 0
    assert_select "a[href=?]", user_path(@user), count: 0
  end

  test "login with remembering" do
    log_in_as(@user, remember_me: '1')
    assert_not_nil cookies['remember_token']
  end

  test "login without remembering" do
    log_in_as(@user, remember_me: '0')
    assert_nil cookies['remember_token']
  end
end

测试/ test_helper.rb中

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActiveSupport::TestCase
  fixtures :all

  # Returns true if a test user is logged in.
  def is_logged_in?
    !session[:user_id].nil?
  end

  # Logs in a test user.
  def log_in_as(user, options = {})
    password    = options[:password]    || 'password'
    remember_me = options[:remember_me] || '1'
    if integration_test?
      post login_path, session: { email:       user.email,
                                  password:    password,
                                  remember_me: remember_me }
    else
      session[:user_id] = user.id
    end
  end

  private

    # Returns true inside an integration test.
    def integration_test?
      defined?(post_via_redirect)
    end
end

测试/集成/ site_layout_test.rb

require 'test_helper'

class SiteLayoutTest < ActionDispatch::IntegrationTest

  test "layout links" do
    get root_path
    assert_template 'static_pages/home'
    assert_select "a[href=?]", root_path, count: 2
    assert_select "a[href=?]", help_path
    assert_select "a[href=?]", about_path
    assert_select "a[href=?]", contact_path
    get signup_path
    assert_select "title", full_title("Sign up")
  end
end

我一团糟..ugh。 任何帮助都将不胜感激!

编辑:另外,我对所有这些都还很陌生,所以如果我要删除旧的测试(因为它们测试绿色),请告诉我。 我不确定这些失败的测试是否会变成红色,因为我做错了什么,或者在阅读本教程时是否应该删除它们。 谢谢!

在你的routes.rb

get 'login'   => 'sessions#new'
get 'login'   => 'sessions#create'

看起来很奇怪。 我想应该是

get 'login'   => 'sessions#new'
post 'login'  => 'sessions#create'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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