Add tests for question destroy use case

This commit is contained in:
Karina Kwiatek 2022-07-08 19:15:09 +02:00 committed by Karina Kwiatek
parent 0e9be78588
commit 58b212f76d
1 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,47 @@
# frozen_string_literal: true
require "rails_helper"
require "errors"
require "use_case/question/destroy"
describe UseCase::Question::Destroy do
subject do
UseCase::Question::Destroy.call(
question_id: question.id,
current_user: current_user
)
end
shared_examples "deletes the question" do
it "deletes the question" do
expect { subject }.to change { Question.count }.by(-1)
end
end
context "question exists" do
let!(:question) { FactoryBot.create(:question, user: question_owner) }
context "user owns question" do
let(:question_owner) { FactoryBot.create(:user) }
let(:current_user) { question_owner }
it_behaves_like "deletes the question"
end
context "user is a moderator" do
let(:question_owner) { FactoryBot.create(:user) }
let(:current_user) { FactoryBot.create(:user, roles: [:moderator]) }
it_behaves_like "deletes the question"
end
context "user does not own question" do
let(:question_owner) { FactoryBot.create(:user) }
let(:current_user) { FactoryBot.create(:user) }
it "raises an error" do
expect { subject }.to raise_error(Errors::Forbidden)
end
end
end
end