2016-12-28 4 views
2

My spec/controllers/decidings_controller_spec.rbは以下のとおりです。Rspecコントローラのテストは通常​​長すぎますか?

RSpec.describe DecidingsController, type: :controller do 
describe '#create' do 
    before do 
     @deciding=build(:deciding_consenting_false) 
    end 
    context 'correct_user login' do 
     before do 
      login_user(@deciding.asking.user) 
     end 
     it 'creates with deciding +1' do 
      expect{post :create , undertaking_id: @deciding.undertaking_id , asking_id: @deciding.asking_id}.to change(Deciding , :count).by(1) 
     end 
     it 'redirects to undertking' do 
      post :create , undertaking_id: @deciding.undertaking_id , asking_id: @deciding.asking_id 
      expect(response).to redirect_to @deciding.undertaking 
     end 
    end 
    context 'incorrect_user login' do 
     before do 
      @user=create(:user) 
      login_user(@user) 
     end 
     it 'creates with deciding 0' do 
      expect{post :create , undertaking_id: @deciding.undertaking_id , asking_id: @deciding.asking_id}.to change(Deciding , :count).by(0) 
     end 
     it 'redirects to undertking' do 
      post :create , undertaking_id: @deciding.undertaking_id , asking_id: @deciding.asking_id 
      expect(response).to redirect_to root_path 
     end 
    end 
end 
describe '#destroy' do 
    context 'before deciding consent' do 
     before do 
      @deciding=create(:deciding_consenting_false) 
     end 
     context 'correct_user login' do 
      before do 
       login_user(@deciding.asking.user) 
      end 
      it 'destroy with deciding -1' do 
       expect{delete :destroy , undertaking_id: @deciding , asking_id: @deciding}.to change(Deciding , :count).by(-1) 
      end 
      it 'redirects undertaking show' do 
       delete :destroy , undertaking_id: @deciding.undertaking_id , asking_id: @deciding.asking_id 
       expect(response).to redirect_to @deciding.undertaking 
      end 
     end 
     context 'incorrect_user login' do 
      before do 
       user=create(:user) 
       login_user(user) 
      end 
      it 'destroy with deciding -1' do 
       expect{delete :destroy , undertaking_id: @deciding , asking_id: @deciding}.to change(Deciding , :count).by(0) 
      end 
      it 'redirects undertaking show' do 
       delete :destroy , undertaking_id: @deciding.undertaking_id , asking_id: @deciding.asking_id 
       expect(response).to redirect_to root_path 
      end 
     end 
    end 
    context ' if after deciding consent(should #before_consent)' do 
     before do 
      @deciding=create(:deciding_consenting_true) 
     end 
     context 'correct_user login' do 
      before do 
       login_user(@deciding.asking.user) 
      end 
      it 'destroy with deciding 0' do 
       expect{delete :destroy , undertaking_id: @deciding , asking_id: @deciding}.to change(Deciding , :count).by(0) 
      end 
      it 'redirects undertaking show' do 
       delete :destroy , undertaking_id: @deciding.undertaking_id , asking_id: @deciding.asking_id 
       expect(response).to redirect_to root_path 
      end 
     end 
    end 
end 
describe '#consent' do 
    context 'correct_user login' do 
     before do 
      @deciding=create(:deciding_consenting_false) 
      login_user(@deciding.undertaking.user) 
     end 
     it 'consenting changes true' do 
      patch :consent , undertaking_id: @deciding.undertaking_id 
      @deciding.reload 
      expect(@deciding.consenting).to eq true 
     end 
     it 'redirect_to undertaking' do 
      patch :consent , undertaking_id: @deciding.undertaking_id 
      expect(response).to redirect_to @deciding.undertaking 
     end 
    end 
    context 'asking user login' do 
     before do 
      @deciding=create(:deciding_consenting_false) 
      login_user(@deciding.asking.user) 
     end 
     it 'consenting not changes true' do 
      patch :consent , undertaking_id: @deciding.undertaking_id 
      @deciding.reload 
      expect(@deciding.consenting).to eq false    
     end 
     it 'redirect_to root' do 
      patch :consent , undertaking_id: @deciding.undertaking_id 
      expect(response).to redirect_to root_path 
     end 
    end 
    context 'incorrect_user login' do 
     before do 
      @deciding=create(:deciding_consenting_false) 
      @user=create(:user) 
      login_user(@user) 
     end 
     it 'consenting not changes true' do 
      patch :consent , undertaking_id: @deciding.undertaking_id 
      @deciding.reload 
      expect(@deciding.consenting).to eq false   
     end 
     it 'redirect_to root' do 
      patch :consent , undertaking_id: @deciding.undertaking_id 
      expect(response).to redirect_to root_path 
     end 
    end 
end 
describe '#deciding_cancel' do 
    context 'correct_user login' do 
     context 'asking user login' do 
      context 'should #after_consent' do 
       before do 
        @deciding=create(:deciding_consenting_true) 
        login_user(@deciding.asking.user) 
       end 

      end 
      context 'should not before consent' do 
       before do 
        @deciding=create(:deciding_consenting_false) 
        login_user(@deciding.asking.user) 
       end     
      end 
      context 'should #before_finish' do 
       before do 
        @deciding=create(:deciding_consenting_true) 
        login_user(@deciding.asking.user) 
       end 

      end 
      context 'should not before finish' do 
       before do 
        @deciding=create(:deciding_after_finish) 
        login_user(@deciding.asking.user) 
       end     
      end 
     end 
     context 'undertaking user login' do 
     end 
    end 
    context 'incorrect_user login' do 
     before do 
      @user=create(:user) 
     end 
    end 
end 


describe '#deciding_cancel' do 
    context 'correct_user login' do 
    end 
    context 'incorrect_user login' do 
    end 
end 
describe '#deciding_cancel' do 
    context 'correct_user login' do 
    end 
    context 'incorrect_user login' do 
    end 
end 

エンド

私Decidingscontrollerは、条件分岐のための多くのbefore_actionを持っています。そのため、スペックファイルが長くなりすぎます。 この場合、別の方法がありますか?プログラムを短くする別の方法がある場合は、教えてください。

とにかく、私のコントローラ/ decidings_controller.rbは以下の通りです。

class DecidingsController < ApplicationController 

#correct_user? 
before_action :authenticate_user! 
before_action :asking_user!, only: [:create , :destroy , :asking_finish , :finish_cancel ] 
before_action :undertaking_user!, only: [:consent , :undertaking_finish] 
before_action :asking_or_undertaking_user! , only: [:deciding_cancel,:deciding_cancel_consent , :deciding_cancel_refuse] 
before_action :reverse_user! ,only: [:deciding_cancel_consent , :deciding_cancel_refuse] 

#流れ 
before_action :before_consent , only: [:destroy] 
before_action :after_consent , only: [:deciding_cancel ,:deciding_cancel_consent , :deciding_cancel_refuse, :undertaking_finish , :asking_finish , :finish_cancel ] 
before_action :before_finish , only: [:deciding_cancel ,:deciding_cancel_consent , :deciding_cancel_refuse] 
before_action :after_deciding_cancel , only: [:deciding_cancel_consent , :deciding_cancel_refuse] 
before_action :after_undertaking_finish , only: [:asking_finish , :finish_cancel] 


def create 
    asking=Asking.find(params[:asking_id]) 
    undertaking=Undertaking.find(params[:undertaking_id]) 
    deciding=Deciding.new(asking_id: asking.id , undertaking_id: undertaking.id) 
    if deciding.save 
     flash[:success] = "契約作成に成功しました。" 
     redirect_to undertaking 
    else 
     flash[:alert] = "契約作成に失敗しました。" 
     redirect_to undertaking 
    end 
end 
def destroy 
    asking=Asking.find(params[:asking_id]) 
    undertaking=Undertaking.find(params[:undertaking_id]) 
    asking.deciding.destroy 
    flash[:info]='契約申し込みをキャンセルしました。' 
    redirect_to undertaking 
end 
def consent 
    undertaking=Undertaking.find(params[:undertaking_id]) 
    if undertaking.deciding 
     undertaking.deciding.update(consenting: true) 
     flash[:success] = "契約が成立しました。" 
     redirect_to undertaking 
    else 
     redirect_to root_path 
    end 
end 
def deciding_cancel 
    undertaking=Undertaking.find(params[:undertaking_id]) 
    undertaking.deciding.update(cancel: true , cancel_userid: current_user.id) 
    flash[:success] = "途中終了リクエストを送信しました。" 
    redirect_to undertaking 
end 
def deciding_cancel_consent 
    undertaking=Undertaking.find(params[:undertaking_id]) 
    undertaking.deciding.destroy   
    flash[:success] = "契約が途中終了されました。" 
    redirect_to undertaking 
end 
def deciding_cancel_refuse 
    undertaking=Undertaking.find(params[:undertaking_id]) 
    undertaking.deciding.update(cansel: false , cansel_userid: nil)  
    flash[:success] = "契約の途中終了を却下しました。" 
    redirect_to undertaking 
end 
def undertaking_finish 
    undertaking=Undertaking.find(params[:undertaking_id]) 
    undertaking.deciding.update(finish_undertaking: true) 
    flash[:success] = "納品が完了しました。" 
    redirect_to undertaking 
end 
def finish_cancel 
    undertaking=Undertaking.find(params[:undertaking_id]) 
    undertaking.deciding.update(finish_undertaking: false) 
    flash[:success] = "再納品リクエストを送信しました。" 
    redirect_to undertaking 
end 
def asking_finish 
    undertaking=Undertaking.find(params[:undertaking_id]) 
    undertaking.deciding.update(finish_asking: true) 
    flash[:success] = "納品が成立しました。" 
    redirect_to asking 
end 


private 

def asking_user! 
    asking=Asking.find(params[:asking_id]) 
    unless asking.user==current_user 
     redirect_to root_path 
    end 
end 
def undertaking_user! 
    undertaking=Undertaking.find(params[:undertaking_id]) 
    unless undertaking.user==current_user 
     redirect_to root_path 
    end 
end  
def asking_or_undertaking_user! 
    undertaking=Undertaking.find(params[:undertaking_id]) 
    unless current_user==undertaking.user || current_user==undertaking.asking.user 
     refirect_to root_path 
    end 
end 
def reverse_user! 
    undertaking=Undertaking.find(params[:undertaking_id]) 
    if current_user.id == undertaking.deciding.cansel_userid 
     refirect_to root_path 
    end 
end 

def before_consent 
    undertaking=Undertaking.find(params[:undertaking_id]) 
    if undertaking.deciding.consenting 
     redirect_to root_path 
    end 
end 


def after_consent 
    asking=Asking.find(params[:asking_id]) 
    unless asking.deciding.consenting 
     redirect_to root_path 
    end 
end 

def before_finish 
    undertaking=Undertaking.find(params[:undertaking_id]) 
    if undertaking.deciding.finish_undertaking 
     redirect_to root_path 
    end 
end 

def after_deciding_cancel 
    undertaking=Undertaking.find(params[:undertaking_id]) 
    unless undertaking.deciding.cancel 
     redirect_to root_path 
    end 
end 

def after_undertaking_finish 
    undertaking=Undertaking.find(params[:undertaking_id]) 
    unless undertaking.deciding.finish_undertaking 
     redirect_to root_path 
    end 
end  

エンド

私はRailsの中に長くコントローラとスペックファイルを心配しないだろう...

+0

ないように長いのすべての長さです。私のRspecのいくつかを見てください! –

+0

'undertaking = Undertaking.find(params [:undertaking_id])'がたくさんあります。おそらくそれを別の 'before_action'呼び出しに変更することができます。熱心な読み込みを検討したいかもしれませんが、あなたのコードを素早く読んでからは、各アクションについての1つの記録/尋ねることの記録に取り組んでいます。 – jvnill

+1

ohとrspecテストは、スタブ/モックや期待のためにテストしているファイルよりも通常長いため、汗をかくことはありません。 – jvnill

答えて

4

を助けてください。

これらのファイルは非常に長くなる傾向があり、クラスとメソッドを短く保つための通常のアドバイスは、コントローラとその仕様に必ずしも適用されません。

たとえば、当社の生産システムでは、user_controller.rbは8500行で、対応するuser_controller_spec.rbは7000行です。

これが私たちのトップ10のコントローラ

1285 app/controllers/*********_controller.rb 
1430 app/controllers/***********_controller.rb 
1444 app/controllers/****_controller.rb 
1950 app/controllers/****_controller.rb 
1994 app/controllers/********_controller.rb 
2530 app/controllers/***********_controller.rb 
2697 app/controllers/*********_controller.rb 
2998 app/controllers/*****_controller.rb 
3134 app/controllers/application_controller.rb 
8737 app/controllers/users_controller.rb 
+0

ああ、それは非常に長いです!ありがとう、私は安心しています! –

+0

コントローラテストのサイズについて知っておくと便利です –

関連する問題