2015-06-29 28 views
0

我有一個測試,試圖查看不存在的訂閱。我與我的燈具中的一羣用戶一起運行這個測試。對於管理員角色中的用戶,當應用程序到達嘗試呈現響應的位置時,它已將動作從:show更改爲:edit,並丟棄了id參數。然而,當我嘗試使用byebug來追蹤執行時,我似乎無法確定它何時發生。爲什麼我的測試嘗試呈現不同的無效路線?

我的測試是:

test "#{u.role} can not view subscriptions that don't exist" do 
    self.send('sign_in_' + u.role) 
    get :show, id:1234567890 
    assert_redirected_to root_path 
    assert_includes flash[:alert], "That subscription doesn't exist" 
end 

其中u是從我的燈具裝用戶。

我得到的錯誤是:

SubscriptionsControllerTest#test_admin_can_not_view_subscriptions_that_don't_exist: 
ActionView::Template::Error: No route matches {:action=>"edit", :controller=>"subscriptions", :id=>nil} missing required keys: [:id] 
    app/views/subscriptions/show.html.erb:13:in `_app_views_subscriptions_show_html_erb__1518678276755260966_70268849069860' 
    test/controllers/subscriptions_controller_test.rb:58:in `block (2 levels) in <class:SubscriptionsControllerTest>' 

我的控制器看起來是這樣的:

class SubscriptionsController < ApplicationController 
    load_and_authorize_resource except: [:create,:new] 
    before_action :set_subscription 
    def show 
    end 
    def edit 
    end 
... 
    private 
    def subscription_params 
     params.require(:subscription).permit(:email,:confirmed) 
    end 
    def set_subscription 
     #byebug if user_signed_in? && current_user.role == 'admin' && self.action_name == 'show' 
     begin 
      if (params.has_key? :id) && (controller_name == 'subscriptions') 
      @subscription = Subscription.find(params[:id]) 
      elsif user_signed_in? 
      @subscription = current_user.subscription || Subscription.new(email: current_user.email) 
      else 
      @subscription = Subscription.new 
      end 
     rescue ActiveRecord::RecordNotFound 
      @subscription = Subscription.new 
      flash.alert = "That subscription doesn't exist" 
     end 
    end 
end 

load_and_authorize_resource來自cancancan。

我與此相關的測試路線是:

resources :subscriptions do 
    member do 
     get 'confirm' 
    end 
    end 

我真的不知道從哪裏何去何從,所以任何建議,將不勝感激。

回答

1

看看堆棧跟蹤此異常:

SubscriptionsControllerTest#test_admin_can_not_view_subscriptions_that_don't_exist: 
ActionView::Template::Error: No route matches {:action=>"edit", :controller=>"subscriptions", :id=>nil} missing required keys: [:id] 
    app/views/subscriptions/show.html.erb:13:in `_app_views_subscriptions_show_html_erb__1518678276755260966_70268849069860' 
    test/controllers/subscriptions_controller_test.rb:58:in `block (2 levels) in <class:SubscriptionsControllerTest>' 

app/views/subscriptions/show.html.erb 13行你打電話link_to(或類似的helper方法)與nil ID,也許?

+0

Doh,不能相信我錯過了。 –

0

看看你的錯誤信息。它說id參數丟失。你可能會給它一個nil的價值。因此,路由器無法正確路由請求。

另外,錯誤是針對對edit操作的請求,但您顯示的代碼是調用show操作。你能清理顯示的代碼示例和錯誤消息並使它們一致嗎?

+0

其實這就是讓我感到困惑的問題,我得到一個錯誤,因爲我沒有提供一個id到編輯路由,當我明顯正在使用id顯示GET時,並且byebug證實了這一點,但我仍然得到錯誤。 –

+1

您嘗試鏈接到編輯網址的'app/views/subscriptions/show.html.erb'模板的第13行有一個鏈接,但沒有'id'值。例如'edit_subscription_path'與'edit_subscription_path(@subscription)' – blowmage

相關問題