Skip to content

Commit

Permalink
2262/purchase error messages (#2267)
Browse files Browse the repository at this point in the history
* Adds context block for incorrect purchase creation tests, adds test for failed purchase creation error display

* Changes purchases#create error flash to display full error messages

* Updates purchase POST#create failure test to assert inclusion of full error messages in response.
  • Loading branch information
PhilipDeFraties committed Apr 6, 2021
1 parent 87dcaa2 commit fb41a89
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/controllers/purchases_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def create
else
load_form_collections
@purchase.line_items.build if @purchase.line_items.count.zero?
flash[:error] = "There was an error starting this purchase, try again?"
flash[:error] = "Failed to create purchase due to: #{@purchase.errors.full_messages}"
Rails.logger.error "[!] PurchasesController#create ERROR: #{@purchase.errors.full_messages}"
render action: :new
end
Expand Down
4 changes: 2 additions & 2 deletions spec/requests/purchases_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
it "renders GET#new with error" do
post purchases_path(default_params.merge(purchase: { storage_location_id: nil, amount_spent_in_cents: nil }))
expect(response).to be_successful # Will render :new
expect(response).to have_error(/error/i)
expect(response.body).to include('Failed to create purchase due to: ["Storage location must exist", "Vendor must exist", "Amount spent in cents is not a number"]')
end
end
end
Expand Down Expand Up @@ -243,4 +243,4 @@
end
end
end
end
end
27 changes: 17 additions & 10 deletions spec/system/purchase_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,23 @@
expect(Purchase.last.line_items.first.quantity).to eq(16)
end

# Bug fix -- Issue #71
# When a user creates a purchase without it passing validation, the items
# dropdown is not populated on the return trip.
it "items dropdown is still repopulated even if initial submission doesn't validate" do
item_count = @organization.items.count + 1 # Adds 1 for the "choose an item" option
expect(page).to have_css("#purchase_line_items_attributes_0_item_id option", count: item_count + 1)
click_button "Save"

expect(page).to have_content("error")
expect(page).to have_css("#purchase_line_items_attributes_0_item_id option", count: item_count + 1)
context 'when creating a purchase incorrectly' do
# Bug fix -- Issue #71
# When a user creates a purchase without it passing validation, the items
# dropdown is not populated on the return trip.
it "items dropdown is still repopulated even if initial submission doesn't validate" do
item_count = @organization.items.count + 1 # Adds 1 for the "choose an item" option
expect(page).to have_css("#purchase_line_items_attributes_0_item_id option", count: item_count + 1)
click_button "Save"

expect(page).to have_content("Failed to create purchase due to:")
expect(page).to have_css("#purchase_line_items_attributes_0_item_id option", count: item_count + 1)
end

it "should display failure with error messages" do
click_button "Save"
expect(page).to have_content('Failed to create purchase due to: ["Vendor must exist", "Amount spent in cents must be greater than 0"]')
end
end
end

Expand Down

0 comments on commit fb41a89

Please sign in to comment.