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

Raise exception if dividing by 0 #3305

Merged
merged 1 commit into from
Aug 23, 2019
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
5 changes: 4 additions & 1 deletion core/app/models/spree/unit_cancel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ def compute_amount(line_item)
private

def weighted_line_item_amount(line_item)
line_item.total_before_tax / quantity_of_line_item(line_item)
quantity_of_line_item = quantity_of_line_item(line_item)
raise ZeroDivisionError, "Line Item does not have any inventory units available to cancel" if quantity_of_line_item.zero?

line_item.total_before_tax / quantity_of_line_item
end

def quantity_of_line_item(line_item)
Expand Down
5 changes: 5 additions & 0 deletions core/spec/models/spree/unit_cancel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
it "divides the line item total by the uncanceled units size" do
expect(subject).to eq(-10.0)
end

it "raises an error if dividing by 0" do
inventory_unit.cancel!
expect { subject }.to raise_error ZeroDivisionError, "Line Item does not have any inventory units available to cancel"
end
end

context "it is called with a line item that doesnt belong to the inventory unit" do
Expand Down