2017-10-29 264 views
0

麻煩我正在做我的第一個Rails應用程序,所以這可能是基本的。與Rails控制器(集成?)測試

在我的用戶配置文件中,我想讓用戶添加標題和說明。我讓他們User模型的屬性,並添加這routes.rb

resources :users do 
    member do 
     post 'update_description' 
    end 
    end 

同樣的方法(不寫還)會處理這兩個屬性。爲了練習TDD,我想編寫一個測試,簡單地表明,如果用戶提交標題,則控制器將其保存到數據庫中。我認爲這將是一個整合測試,但我無法找到正確的路徑。 (應該它是一個集成測試嗎?)但是,隨着研究,我設法寫一個工作post語句在一個相關的控制器測試文件。這裏的控制器測試:

test "profile submits new title and description successfully" do 
    log_in_as(@user) 
    get :show, id: @user 
    assert_nil @user.title 
    post :update_description, id: @user, params: { title: "Lorem ipsum" } 
    # Next: 
    # @admin.reload.title 
    # assert @admin.title == "Lorem ipsum" 
    # assert_template 'users/show' 
    # Etc. 

    end 

這引起以下錯誤:

ERROR["test_profile_submits_new_title_and_description_successfully", UsersControllerTest, 2017-10-22 21:42:52 -0400] 
test_profile_submits_new_title_and_description_successfully#UsersControllerTest (1508722972.17s) 
ActionView::MissingTemplate:   ActionView::MissingTemplate: Missing template users/update_description, application/update_description with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: 
      * "/var/lib/gems/2.3.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates" 
      * "/home/globewalldesk/startthis/app/views" 
      * "/var/lib/gems/2.3.0/gems/web-console-2.0.0.beta3/app/views" 

      test/controllers/users_controller_test.rb:83:in `block in <class:UsersControllerTest>' 
     test/controllers/users_controller_test.rb:83:in `block in <class:UsersControllerTest>' 

我推測,這意味着Rails正在尋找一個視圖文件,並不能找到一個,但我沒有看到爲什麼post :update_description它尋找一個視圖...我認爲這將張貼信息沒有一個視圖(我有一個類似的路徑,以相同的方式,沒有看法)。 update_description方法在Users控制器中。我做了很多研究,但我無法弄清楚我做錯了什麼。幫幫我! TIA。

回答

1

您編寫測試的方式看起來像集成測試。但我個人會建議編寫一個系統測試。因爲我看到你創建一個update_description成員路由只是爲了更新用戶對象?那沒有必要 - 您的User資源已有editupdate操作,因此您可以刪除該成員路由。

集成測試用於檢查工作流程以及如何交互應用程序的不同部分。雖然系統檢查是針對用戶交互的,但基本上你會檢查用戶在瀏覽器中執行的操作。此外,在我看來用這種技術編寫測試要簡單得多(至少在這個級別上)。

所以,你的系統測試會是什麼樣子:

setup do 
    log_in_as(@user) // or what ever code to visit login page and login user 
end 

test "profile submits new title successfully" do 
    visit edit_user_path 

    fill_in "Title", with: "Lorem ipsum" 

    click_on "Save" 

    assert_response :redirect 
    follow_redirect! 
    assert_select "Title", text: "Lorem ipsum" 
end 

這假設用戶提交了表格後,應用重定向到user_path(@user)(顯示頁)。

,還是集成測試看起來是這樣的:

test "profile submits new title successfully" do 
    log_in_as(@user) // or what ever code to login user 
    get "/user/#{@user.id}/edit" 
    assert_response :success 

    updated_title = "Lorem ipsum" 

    patch :update, user: { id: @user.id, title: updated_title } 

    assert_response :redirect 
    follow_redirect! 
    assert_response :success 
    assert_select "Title", text: "Lorem ipsum" 
end 

注意 - 我沒有測試過這一點,我用的水豚等工具,但不MINITEST。但在這簡單這應該工作,我認爲。

並檢查docs如果您還沒有這麼做了..

+0

哇,我一直在尋找網上很多過去幾天的這樣做的JavaScript測試。我聽說過的一個測試框架是Capybara,在這裏你鏈接到Rails Guides頁面,它說:「系統測試使用Capybara作爲基礎。」你知道我使用的是Rails 4.2.2嗎?無論如何,我會嘗試你的解決方案,至少對於集成測試。系統測試看起來是另一件要學習的東西(很快)。謝謝! – globewalldesk

+0

所以,下面是導致我創建'update_description'路線而不是依賴'Users#edit'的問題。用戶編輯頁面(位於'users//edit')具有名稱和密碼。個人資料頁面位於'users/'處。我想讓用戶可以添加profile-y類型的信息,如標題,描述和接收消息。所以我想......那些是'用戶'屬性,對吧?但是我已經有了一個'edit'動作來處理用戶名和密碼信息。所以我只需製作一條新路線來處理用戶信息的編輯。 有沒有更好的解決方案? – globewalldesk

+0

感謝您的幫助。我弄錯了兩件事。首先,在集成測試中,我使用了錯誤的「post」行。這工作: 'post「/users/#{@non_admin.id}/update_description」, PARAMS:{title:「我是不斷悲傷的人。」 }' 而另一個問題是'用戶#update_description'操作應該加載_some_視圖(我還沒有寫出方法!)。我只是添加了'redirect_to user_path(@user)',它工作! – globewalldesk