Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the issue where the exception was not caught when the purchase was distributed. #4894

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions app/controllers/purchases_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,14 @@ def update

def destroy
purchase = current_organization.purchases.find(params[:id])
PurchaseDestroyService.call(purchase)
begin
PurchaseDestroyService.call(purchase)
rescue => e
flash[:error] = e.message
else
flash[:notice] = "Purchase #{params[:id]} has been removed!"
end

flash[:notice] = "Purchase #{params[:id]} has been removed!"
redirect_to purchases_path
end

Expand Down
30 changes: 23 additions & 7 deletions spec/system/purchase_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,30 @@
sign_in organization_admin
end

it "allows deletion of a purchase" do
visit "#{subject}/#{purchase.id}"
expect(page).to have_link("Delete")
accept_confirm do
click_on "Delete"
context "When the purchase remains in storage location" do
it "allows deletion of a purchase" do
visit "#{subject}/#{purchase.id}"
expect(page).to have_link("Delete")
accept_confirm do
click_on "Delete"
end
expect(page).to have_content "Purchase #{purchase.id} has been removed!"
expect(page).to have_content "0 (Total)"
end
end

context "When the purchase has been distributed" do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really all we're testing here is the ability to catch errors. So you can ensure that the delete service throws an error:

allow(PurchaseDestroyService).to receive(:call).and_raise('an error')

and you don't need all the setup here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's really easy. I will change it. Thanks for your review.

it "delete a purchase should get an error" do
allow(PurchaseDestroyService).to receive(:call).with(purchase).and_raise(InventoryError)

visit "#{subject}/#{purchase.id}"
expect(page).to have_link("Delete")
accept_confirm do
click_on "Delete"
end

expect(page).to have_css(".alert.error.alert-danger")
end
expect(page).to have_content "Purchase #{purchase.id} has been removed!"
expect(page).to have_content "0 (Total)"
end
end
end
Loading