簡體   English   中英

RubySpec在Ruby控制器中測試重定向

[英]RubySpec to test a redirect in Ruby controller

我正在嘗試為用戶訪問提供者/計划和提供者/潛在客戶時重定向到特定路徑的方法編寫規范

在之前的組中,我嘗試了許多路徑組合,在嘗試中。

before { get :provider_plans_index_path }
before { get :provider_index_plans_path}
before { get :provider_plans_path}
before { get :provider_index_path}
before { get :provider_plans_path}
before { get :provider_planes_path}

base_controller.rb

class Provider::BaseController < ActionController::Base
  layout 'provider'

  before_action :allowed_pages

  def allowed_pages    
    redirect_to financial_dashboard_path if  !requested_action?(params[:controller])
  end

  def requested_action?(data)
    regexp = %r{
      ^(provider/plans)|      
      (provider/prospects)$
    }x
    data.match?(regexp)
  end
end

base_controller_spec.rb

require 'rails_helper'

describe Provider::BaseController, type: :controller do
  let(:provider) { create(:provider) }
  let(:financial) { create(:financial, provider: provider) }
  let(:user) { provider.user }
  before { login_user user }

  describe 'GET plans' do
    context 'with not allowed url' do      
      before { get :provider_planes_path}
      it { should redirect_to financial_dashboard_path}
    end
  end
end

routes.rb

namespace :provider do
  get '', to: 'dashboard#index'
  get 'dashboard', to: 'dashboard#index'

  resources :plans, only: [:index, :create, :update], path: 'planes'
  resources :prospects,  only: [:index, :show, :create, :update], path: 'prospectos' do
    get 'diagnostico', to: 'prospects#show', on: :member, as: :general
    patch 'diagnostico', to: 'prospects#update', on: :member
    get 'configuracion', to: 'prospects#configuration', on: :member, as: :configuration
  end
end

我在所有組合中都遇到此錯誤

ActionController :: UrlGenerationError:沒有路由匹配

對我來說,解決方案是通過另一個控制器調用規范中的動作

context 'with not allowed url' do
  before do
   @controller = Provider::PlansController.new
   get :index
  end
  it { should redirect_to financial_dashboard_path }
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM