2015-08-22 193 views
1

測試我一直收到三個錯誤,我不知道爲什麼。我會盡我所能地跟隨教程,並且我已經通過第9章學習了錯誤,並且我還沒有看到任何錯誤。運行bundle exec rake test。我得到了以下三個錯誤:Ruby on Rails的教程第3版,第9章:失敗的users_edit_test.rb

FAIL["test_unsuccessful_edit", UsersEditTest, 2015-08-22 09:09:06 +0900] 
test_unsuccessful_edit#UsersEditTest (1440202146.96s) 
     expecting <"users/edit"> but rendering with <[]> 
     test/integration/users_edit_test.rb:12:in `block in <class:UsersEditTest>' 

FAIL["test_successful_edit", UsersEditTest, 2015-08-22 09:09:06 +0900] 
test_successful_edit#UsersEditTest (1440202146.97s) 
     expecting <"users/edit"> but rendering with <[]> 
     test/integration/users_edit_test.rb:25:in `block in <class:UsersEditTest>' 

FAIL["test_successful_edit_with_friendly_forwarding", UsersEditTest, 2015-08-22 09:09:07 +0900] 
test_successful_edit_with_friendly_forwarding#UsersEditTest (1440202147.07s) 
     Expected response to be a redirect to <http://www.example.com/users/762146111/edit> but was a redirect to <http://www.example.com/>. 
     Expected "http://www.example.com/users/762146111/edit" to be === "http://www.example.com/". 
     test/integration/users_edit_test.rb:43:in `block in <class:UsersEditTest>' 

正如你所看到的,我的錯誤,可在測試/集成/ users_edit_test找到。我的兩個測試都以相同的方式失敗,並顯示以下消息:expecting <"users/edit"> but rendering with <[]>。第12行和25具有相同的代碼:

assert_template 'users/edit' 

線43具有下面的代碼:

assert_redirected_to edit_user_path(@user) 

我有一個應該去用戶的一個鏈接_header.html.erb文件編輯頁面,但它只是重定向到索引。我有一個文件app/views/users/edit.html.erb,所以我不知道它爲什麼沒有顯示出來。下面是設置鏈接代碼:

<li><%= link_to "Settings", edit_user_path(current_user) %></li>

這是我爲我的users_controller.rb文件的代碼。我懷疑問題是我的correct_user方法沒有按照它應該的那樣發射。 (順便說一句,我曾嘗試註釋掉「編輯」方法中的代碼,但問題依然存在。)

class UsersController < ApplicationController 
    before_action :logged_in_user, only: [:index, :edit, :update, :destroy] 
    before_action :correct_user, only: [:edit, :update] 
    before_action :admin_user, only: :destroy 

    def index 
    @users = User.paginate(page: params[:page]) 
    end 

    def show 
    @user = User.find(params[:id]) 
    # debugger 
    end 

    def new 
    @user = User.new 
    end 

    def create 
    @user = User.new(user_params) 
    if @user.save 
     log_in @user 
     flash[:success] = "Welcome to the Sample App!" 
      redirect_to @user 
    else 
     render 'new' 
    end 
    end 

    def edit 
    # @user = User.find(params[:id]) 
    end 

    def update 
    if @user.update_attributes(user_params) 
     flash[:success] = "Profile updated" 
     redirect_to @user 
    else 
     render 'edit' 
    end 
    end 

    def destroy 
    User.find(params[:id].destroy) 
    flash[:success] = "User deleted" 
    redirect_to users_url 
    end 

    private 

    def user_params 
     params.require(:user).permit(:name, :email, :password, :password_confirmation) 
    end 

    def logged_in_user 
     unless logged_in? 
     store_location 
     flash[:danger] = "Please log in." 
     redirect_to login_url 
     end 
    end 

    def correct_user 
     @user = User.find(params[:id]) 
    # redirect_to(root_url) unless @user == current_user 
     redirect_to(root_url) unless @user == current_user?(@user) 
    end 

    def admin_user 
     redirect_to(root_url) unless current_user.admin? 
    end 
end 

在app /傭工/ sesions_helper.rb我有兩種方法,current_usercurrent_user?

def current_user?(user) 
    user == current_user 
end 

def current_user 
    if(user_id = session[:user_id]) 
     @current_user ||= User.find_by(id: user_id) 
    elsif (user_id = cookies.signed[:user_id]) 
     user = User.find_by(id: user_id) 
     if user && user.authenticated?(cookies[:remember_token]) 
      log_in user 
      @current_user = user 
     end 
    end 
end 

下面是配置/ routes文件:

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' 
    post 'login' => 'sessions#create' 
    delete 'logout' => 'sessions#destroy' 
    resources :users 

我懷疑回答我的問題是超級容易,但我不知道什麼是錯的。這裏是users_edit_test.rb文件

require 'test_helper' 

class UsersEditTest < ActionDispatch::IntegrationTest 
    def setup 
    @user = users(:michael) 
    end 

    test "unsuccessful edit" do 
# log_in_as(@user) 
    get edit_user_path(@user) 
    log_in_as(@user) 
    assert_template 'users/edit' 
    patch user_path(@user), user: { name: '', 
            email: '[email protected]', 
            password: 'foo', 
            password_confirmation: 'bar' } 
    assert_template 'users/edit' 
    end 


    test "successful edit" do 
# log_in_as(@user) 
    get edit_user_path(@user) 
    log_in_as(@user) 
    assert_template 'users/edit' 
    name = "Foo Bar" 
    email = "[email protected]" 
    patch user_path(@user), user: { name: name, 
            email: email, 
            password: '', 
            password_confirmation: '' } 
    assert_not flash.empty? 
    assert_redirected_to @user 
    @user.reload 
    assert_equal @user.name, name 
    assert_equal @user.email, email 
    end 

    test "successful edit with friendly forwarding" do 
    log_in_as(@user) 
    get edit_user_path(@user) 
# log_in_as(@user) 
    assert_redirected_to edit_user_path(@user) 
    name = "Foo Bar" 
    email = "[email protected]" 
    patch user_path(@user), user: { name: name, 
        email: email, 
        password: 'foobar', 
        password_confirmation: 'foobar' } 
    assert_not flash.empty? 
    assert_redirected_to @user 
    @user.reload 
    assert_equal @user.name, name 
    assert_equal @user.email, email 
    end 
end 
+0

請更新您的問題。 –

+0

爲什麼編輯方法中的@user被註釋掉了? – engineerDave

+0

@engineerDave因爲在編輯方法之前運行的correct_user方法在使用編輯或更新方法時執行相同的任務。 –

回答

4

雖然,你沒有上傳users_edit_test.rb文件在這裏,其實我讀你所提到的,然後書chapter 9也得到了源代碼,從它的github repo

所以,我和你(bundle exec rake test test/integration/users_edit_test.rb)本地運行相同的測試,它的傳球很好。沒有測試失敗。但是,我發現如果​​從test "unsuccessful edit"測試中缺失,那麼它會失敗,並顯示與您在此處發佈的完全相同的錯誤消息。而且,這是有道理的,因爲如果你沒有​​即LOGGED_IN用戶,那麼就無法呈現該用戶編輯頁面,因此得到這個錯誤:

expecting <"users/edit"> but rendering with <[]> 

所以,我敢肯定您從教程中輸入時錯過了該部分。

這裏是完整的工作版本(所有測試都通過)的users_edit_test.rb文件的

require 'test_helper' 

class UsersEditTest < ActionDispatch::IntegrationTest 

    def setup 
    @user = users(:michael) 
    end 

    test "unsuccessful edit" do 
    log_in_as(@user) 
    get edit_user_path(@user) 
    assert_template 'users/edit' 
    patch user_path(@user), user: { name: "", 
            email: "[email protected]", 
            password:    "foo", 
            password_confirmation: "bar" } 
    assert_template 'users/edit' 
    end 

    test "successful edit with friendly forwarding" do 
    get edit_user_path(@user) 
    log_in_as(@user) 
    assert_redirected_to edit_user_path(@user) 
    name = "Foo Bar" 
    email = "[email protected]" 
    patch user_path(@user), user: { name: name, 
            email: email, 
            password:    "", 
            password_confirmation: "" } 
    assert_not flash.empty? 
    assert_redirected_to @user 
    @user.reload 
    assert_equal name, @user.name 
    assert_equal email, @user.email 
    end 
end 

只需複製這一點,並替換當前的文件與此內容。然後,所有的測試都會通過。

+0

我嘗試了你所說的,我收到了同樣的錯誤。 –

+0

那麼,你在其他地方還有其他一些錯字/缺失部分。因爲,它在我的電腦中完美傳遞給我。所以,我們必須弄清楚你錯過了什麼。 –

+0

因此,在替換文件後,仍然像以前一樣得到全部3個錯誤? –

0

我通過本教程現在的工作,以及,我得到了測試通過就好了,所以我比較我的測試,以你的,你需要做以下修改:

  1. successful testunsuccessful test,在get edit_user_path(@user)之前輸入​​。
  2. successful test with friendly forwarding,正好相反:把​​get edit_user_path(@user)
  3. 在你users_controller.rb文件,檢查您的私人correct_user方法:

    redirect_to(root_url) unless @user == current_user?(@user) 
    

    應該

    redirect_to(root_url) unless current_user?(@user) 
    

進行這些更改並重新運行測試套件應該讓你的測試通過。

0

我下面的教程一樣,我得到了同樣的錯誤:

expecting <"users/edit"> but rendering with <[]> 

我剛剛與這個塊的位置的開關解決了這個問題:

test "unsuccesful edit" do 
    log_in_as(@user) 
    get edit_user_path(@user) 
    assert_template 'users/edit' 
    patch user_path(@user), user: { name: "", 
            email: "[email protected]", 
            password: "foo", 
            password_confirmation: "bar" } 
    assert_template 'users/edit' 
    end 

這一個:

test "successful edit" do 
    log_in_as(@user) 
    get edit_user_path(@user) 
    assert_template 'users/edit' 
    name = "Name" 
    email = "[email protected]" 
    patch user_path(@user), user: { name: name, 
            email: email, 
            password: "", 
            password_confirmation: "" } 
    assert_not flash.empty? 
    assert_redirected_to @user 
    @user.reload 
    assert_equal name, @user.name 
    assert_equal email, @user.email 
    end 

其實現在它以任何順序工作我把這兩個塊..我知道它沒有任何意義,但也許它是鏈接w在Cloud9(作者建議使用的基於雲的IDE)上粘貼一些奇怪的行爲&。

你是怎麼解決這個問題的呢?

0

我正在通過教程工作。我有一些類似的問題。

如果我理解正確<[]>表示測試瀏覽器尚未遵循的重定向。我會在失敗的斷言之前放置一個debugger,並從那裏檢查情況。在我的情況下,被遺忘的follow_redirect!assert_template而不是assert_redirected_to是原因。

0

其實我也遇到了同樣的問題。 在我的情況下,事實證明我更改了用戶在夾具​​user.yml中的密碼。

幾個小時後,我發現必須有之間的強alignement:

  1. user.yml
  2. test_helper.rb2。
  3. 數據庫(其中摘要存儲)
1

讓您users.yml裏是明確界定和有沒有一個錯字了它。我有一個錯字,並且遇到了同樣的錯誤。

錯誤(請注意在電子郵件上E)

michael: 
    name: michael Example 
    email: [email protected] 
    password_digest: <%= User.digest('password') %> 

明確界定與`測試/集成/ users_edit_test.rb`文件的內容

michael: 
    name: michael Example 
    email: [email protected] 
    password_digest: <%= User.digest('password') %> 
相關問題