From 18421df9d040a77a3091968a8ffacc1494da58d4 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Tue, 21 Feb 2023 09:50:28 +0100 Subject: [PATCH] Add tests for `QuestionController` --- spec/controllers/question_controller_spec.rb | 62 ++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 spec/controllers/question_controller_spec.rb diff --git a/spec/controllers/question_controller_spec.rb b/spec/controllers/question_controller_spec.rb new file mode 100644 index 00000000..c8daf340 --- /dev/null +++ b/spec/controllers/question_controller_spec.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +require "rails_helper" + +describe QuestionController, type: :controller do + describe "#show" do + subject { get :show, params: { id: question.id, username: question.user.screen_name } } + + before do + stub_const("APP_CONFIG", { + "site_name" => "Specspring", + "hostname" => "test.host", + "https" => false, + "items_per_page" => 10, + }) + end + + context "question exists" do + let(:question) { FactoryBot.create(:question, user: FactoryBot.create(:user)) } + + context "no answers" do + it "renders an empty list" do + expect(subject).to have_http_status(:ok) + expect(assigns(:question)).to eq(question) + expect(assigns(:answers)).to be_empty + expect(assigns(:answers_last_id)).to be_nil + expect(assigns(:more_data_available)).to eq(false) + end + end + + context "some answers" do + before do + num_answers.times do + FactoryBot.create(:answer, question:, user: FactoryBot.create(:user)) + end + end + + let(:num_answers) { 10 } + + it "renders a list of questions" do + expect(subject).to have_http_status(:ok) + expect(assigns(:question)).to eq(question) + expect(assigns(:answers).length).to eq(10) + expect(assigns(:answers_last_id)).to_not be_nil + expect(assigns(:more_data_available)).to eq(false) + end + + context "enough answers to paginate" do + let(:num_answers) { 11 } + + it "renders a list of questions" do + expect(subject).to have_http_status(:ok) + expect(assigns(:question)).to eq(question) + expect(assigns(:answers).length).to eq(10) + expect(assigns(:answers_last_id)).to_not be_nil + expect(assigns(:more_data_available)).to eq(true) + end + end + end + end + end +end