From f5f7b0d22b8e0b741068a29521d77a850330bd89 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 20 Aug 2022 17:07:37 +0200 Subject: [PATCH] Add tests for `User#questions` action and cases --- spec/controllers/user_controller_spec.rb | 52 +++++++++++++++++++++--- spec/factories/question.rb | 1 + 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/spec/controllers/user_controller_spec.rb b/spec/controllers/user_controller_spec.rb index dfe28009..0027d6ac 100644 --- a/spec/controllers/user_controller_spec.rb +++ b/spec/controllers/user_controller_spec.rb @@ -50,15 +50,57 @@ describe UserController, type: :controller do end describe "#questions" do + let!(:question) { FactoryBot.create(:question, user: user, direct: true, author_is_anonymous: false) } subject { get :questions, params: { username: user.screen_name } } - context "user signed in" do - before(:each) { sign_in user } + it "renders the user/questions template" do + subject + expect(assigns(:user)).to eq(user) + expect(response).to render_template("user/questions") + end - it "renders the user/questions template" do + context "current user signed in" do + before do + sign_in user + end + + it "renders all questions" do subject - expect(assigns(:user)).to eq(user) - expect(response).to render_template("user/questions") + expect(assigns(:questions).size).to eq(1) + end + end + + context "user signed in" do + let(:another_user) { FactoryBot.create :user } + + before do + sign_in another_user + end + + it "renders no questions" do + subject + expect(assigns(:questions).size).to eq(0) + end + end + + context "moderator unmasked" do + let(:another_user) { FactoryBot.create :user, roles: ["moderator"] } + + before do + sign_in another_user + allow_any_instance_of(UserHelper).to receive(:moderation_view?) { true } + end + + it "contains all questions" do + subject + expect(assigns(:questions).size).to eq(1) + end + end + + context "user not signed in" do + it "contains no questions" do + subject + expect(assigns(:questions).size).to eq(0) end end end diff --git a/spec/factories/question.rb b/spec/factories/question.rb index d5474c8b..8c371041 100644 --- a/spec/factories/question.rb +++ b/spec/factories/question.rb @@ -5,5 +5,6 @@ FactoryBot.define do user { nil } content { Faker::Lorem.sentence } author_is_anonymous { true } + direct { true } end end