Add tests for question destroy use case
This commit is contained in:
parent
0e9be78588
commit
58b212f76d
|
@ -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
|
Loading…
Reference in New Issue