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
、レコードがnilでない既存のcompany = Company.find_by(id: accountant_request.company_id)
戻ります。だから私は内部のif文を調べることができなかった。
どういうところが間違っていますか?この場合、私は何をすべきですか? アイデアを歓迎します。
コメントありがとうございます。わかった。来週この変更を適用し、その結果をお知らせします。 –
@Samy_Kacimi私はあなたが教えたことをやった。しかし、キー 'index_companies_on_external_cid'に '#
ユーザ用にスタブを作成して修正しました。 –