2017-12-22 3 views
0

이 소스에서 스텁을 만들 수없는 이유를 알 수 없습니다. if 문 내부의 논리를 테스트하려고합니다. if user.nil? || company.nil? 이 조건입니다. 이를 위해 사양 정의를 아래와 같이하였습니다.RSpec에서 스텁하는 방법?

def introduce 
    accountant_request = AccountantRequest.with_introduce_request_status(:requested).find(params[:id]) 

    company = Company.find_by(id: accountant_request.company_id) 
    user = User.find_by(id: company.try(:primary_user_id)) 
    if user.nil? || company.nil? 
     redirect_to admin_accountant_requests_path, alert: 'There is no company or user' and return 
    end 

다음은 사양입니다. 내 기대는 Company.find_by (...)는 nil을 반환합니다.

shared_examples 'post #introduce' do 
    subject(:response) { post 'introduce', params_for_introduce } 

    context 'error by user.nil? || company.nil?' do 
     it 'fail' do 
     allow_any_instance_of(Company).to receive(:find_by).and_return(nil) 
     expect(response).to have_http_status 302 
     expect(response).to redirect_to introduce_error_redirect_path2 
     end 
    end 

불행하게도, company = Company.find_by(id: accountant_request.company_id) 반환 기록이 전무되지 기존. 그래서 if 문 내부를 조사 할 수 없었습니다.

무엇이 잘못 되었나요? 그리고이 경우 어떻게해야합니까? 어떤 아이디어라도 환영합니다.

답변

1

allow_any_instance_of는 클래스 자체

을 시도해보십시오 회사 클래스의 예를에 사용되어야하지만 여기 find_by 에서 호출이 (요청 즉, 시험 대상 실행하기 전에) :

allow(Company).to receive(:find_by) { nil } 
+0

의견을 보내 주셔서 감사합니다. 알 겠어. 다음주에이 수정을 적용하고 결과를 알려 드리겠습니다. –

+0

@Samy_Kacimi 나는 당신이 가르쳐 준 것을했습니다. 그러나 키 'index_companies_on_external_cid'에 대해 '#

+0

사용자를 위해 스텁을 만들었습니다. 감사합니다. –

관련 문제