Skip to content

Commit

Permalink
HW10 gernichenko (fs#127)
Browse files Browse the repository at this point in the history
* added tests for destroy & update

* rubocop fix

* gitignore fix

* minor changes
  • Loading branch information
elenagernichenko authored Nov 26, 2023
1 parent e9763ff commit 11cd4e5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ yarn-debug.log*
.idea/

.env

/dump.rdb
2 changes: 1 addition & 1 deletion spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
factory :user do
first_name { "test" }
last_name { "test" }
email { "[email protected]" }
sequence(:email) { |n| Faker::Internet.email.gsub("@", "-#{n}@") }
role { "member" }
password { "123456" }
end
Expand Down
22 changes: 22 additions & 0 deletions spec/interactors/projects/destroy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "rails_helper"

describe Projects::Destroy do
describe ".call" do
let(:interactor) { described_class.new(project: project, users: users) }
let!(:project) { create(:project) }
let!(:users) { create_list(:user, 3, projects: [project]) }

it "destroys the project" do
expect { interactor.run }.to change(Project, :count).from(1).to(0)
end

it "sends project destruction emails to all users" do
allow(ProjectMailer).to receive(:project_destroyed).and_call_original
interactor.run

users.each do |user|
expect(ProjectMailer).to have_received(:project_destroyed).with(user).once
end
end
end
end
37 changes: 37 additions & 0 deletions spec/interactors/projects/update_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require "rails_helper"

describe Projects::Update do # rubocop:disable Metrics/BlockLength
describe ".call" do
let(:interactor) { described_class.new(context) }
let!(:project) { create(:project) }
let!(:users) { create_list(:user, 3, projects: [project]) }

context "when project update is successful" do
let(:context) { { project: project, project_params: { name: "New Name" } } }

it "updates the project" do
interactor.run
expect(project.reload.name).to eq("New Name")
end

it "sends project update emails to all users" do
allow(ProjectMailer).to receive(:project_updated).and_call_original
interactor.run

users.each do |user|
expect(ProjectMailer).to have_received(:project_updated).with(project, user).once
end
end
end

context "when project update fails" do
let(:context) { { project: project, project_params: { name: "" } } }

it "fails the context with an error message" do
interactor.run
expect(interactor.context.failure?).to be true
expect(interactor.context.error).to eq("Invalid data")
end
end
end
end

0 comments on commit 11cd4e5

Please sign in to comment.