Order and Payment Handling for an Ecommerce Website
In this second part of a five-part series on building the checkout and order processing parts of an ecommerce application in Ruby on Rails, you'll learn how to save order information, integrate with payment gateways, and more. This article is excerpted from chapter nine of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).
Now, let’s continue by using TDD to implement the user story. We want to test two scenarios: one where the cart is empty and one where the cart contains books. We are going to call multiple controllers (theCartandCheckoutcontrollers) from our tests, so we’ll use an integration test instead of a functional test. Create the test with the following command:
Let’s start with the first scenario. When the cart is empty, the user shouldn’t be able to perform a checkout. This can be verified by replacing thetest_truthmethod intest/integration/ checkout_test.rbwith the code shown here:
def test_that_empty_cart_shows_error_message get '/checkout' assert_response :redirect assert_redirected_to :controller => "catalog" assert_equal "Your shopping cart is empty!➥ Please add something to it before proceeding to checkout.", flash[:notice] end
This test verifies that when the cart is empty, we are redirected to the catalog page and an error message is displayed.
If you run the test now, it fails, because we haven’t implemented the controller yet. Openapp/controllers/checkout_controller.rbin your editor and change theindexmethod as follows:
def index @order = Order.new @page_title = "Checkout" if @cart.books.empty? flash[:notice] = "Your shopping cart is empty!➥ Please add something to it before proceeding to checkout." redirect_to :controller => 'catalog' end end
Also add theinitialize_cartfilter to the controller:
class CheckoutController < ApplicationController before_filter :initialize_cart
Recall that this filter was implemented in Chapter 5. It initializes the cart, so that we can access it from theCheckoutcontroller and views. It also enables us to show the shopping cart to the right on the page (also implemented in Chapter 5).
Execute the test with the following command:
$ ruby test/integration/checkout_test.rb
You should see no errors. Perform a manual test by openinghttp://localhost:3000/checkout. Because the shopping cart is empty, you should be redirected to the catalog page, where the error message in Figure 9-1 is displayed.
Figure 9-1. The catalog page displaying an empty cart error message
Next, we’ll add a test for the second scenario. When the shopping cart contains one or more items (books), we want to display a form with three sections: contact information, shipping address, and billing information. First, add the following fixtures and test totest/integration/checkout_test.rb:
The test begins by adding a book (defined in thebooksfixture file) to the cart by calling the/cart/addaction. Then it accesses the checkout page and verifies that the request is successful. It proceeds by checking that the page contains the three required sections. This is done by looking for threelegendtags having the following content:Contact Information,Shipping Address, andBilling Information.
Now that we have the new test in place, we could try to run it, but it would fail miserably when it tries to find the three sections.