Hi, I am trying to write an integration test for a checkout process. From the documentation I found, I understood, that I had to interact in an integration test with the controllers through get and post requests.
I have difficulties in writing an integration test, which covers a complex checkout create action, which does not only create an order, but also create associated order_items.
I wrote a test along these lines
class ShopFlowsTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
test "placing order via wire transfer" do
...
assert_difference('Order.count') do
post orders_path,
params: {
order: {
# Contact Information
...
# Shipping Address
...
# Billing Address
...
#
...
# Shippment details
...
}
}
end
assert_response :redirect
follow_redirect!
assert_redirected_to controller: :shops, action: :index
end
end
For this action:
def create
session[:order_params].deep_merge!(params[:order]) if params[:order]
@order = Order.new(session[:order_params])
@order.populate_order(@cart) # <<<
@order.current_step = session[:order_step]
if @order.valid?
if params[:back_button]
@order.previous_step
elsif @order.last_step?
@order.save if @order.all_valid?
else
@order.next_step
end
session[:order_step] = @order.current_step
end
if @order.new_record?
render "new"
else
session[:order_step] = session[:order_params] = nil
flash[:notice] = "Order saved!"
redirect_to controller: :shops, action: :index
end
end
and get this error message:
FAIL ShopFlowsTest#test_placing_order_via_wire_transfer (1.58s)
"Order.count" didn't change by 1.
Expected: 3
Actual: 2
test/integration/shop_flows_test.rb:18:in `block in <class:ShopFlowsTest>'
I think the error message derives from the fact, that I am not giving the details of the products to the post request. In my app this info comes out of the database and through a method in my order model. How would one test this part in an integration test? I already did a system test, but would like to learn more about integration tests… Can it be done?
Thanks in advance!