2

My orders_controller needs to forward an order to a payment gateway. It's making my tests fail: No route matches [GET] "/v2/checkout/payment.html"

That's is the URL that the PaymentGateway object redirects to. How can I trick my tests into thinking that the payment gateway returned a response? In reality, it doesn't. It may or may not return the user depending on his choice. It's similar to paying with Paypal.

  def create
    @order = current_user.orders.build(params[:order])
    @order.add_line_items_from_cart(current_cart)
    if @order.save
      destroy_cart current_cart
      payment = PaymentGateway.new(@order).send
      redirect_to payment
    else
      render 'new'
    end
  end


feature 'User creates an order with valid info' do

  background do
    setup_omniauth_user
    visit root_path

    create(:line_item, cart: Cart.last)

    click_link 'cart-link'
    click_link 'th-checkout-link'
  end

  scenario 'an order is created and cart is deleted' do
    cart = Cart.last
    fill_in_order

    expect {
      click_button "Submit"
    }.to change(Order, :count)

    cart.reload.should be_nil
  end
end



User creates an order with valid info an order is created and cart is deleted
     Failure/Error: click_button "Submit"
     ActionController::RoutingError:
       No route matches [GET] "/v2/checkout/payment.html"
     # ./spec/features/orders_spec.rb:64:in `block (3 levels) in <top (required)>'
     # ./spec/features/orders_spec.rb:63:in `block (2 levels) in <top (required)>'
dee
  • 1,848
  • 1
  • 22
  • 34

1 Answers1

4

You can use a gem such as WebMock to stub and set the expectation for remote HTTP requests.

I use this to mock payment gateways & single sign-on authentication. Here is an example of syntax usage, although you will obviously need to update the body to reflect what should be returned.

stub_request(:any, "http://example.com/v2/checkout/payment.html").to_return(
  :body => "SUCCESS",
  :status => 200,
  :headers => { 'Content-Length' => 3 }
)
Dan Reedy
  • 1,872
  • 10
  • 13
  • Thanks! I'm finding it hard to see how that would work work with the `expect { click_button "Submit" }.to change(Order, :count)` though... where should I add the request stubb? – dee May 13 '13 at 14:24
  • You'd add the request stub like you would any other stub, either in a setup/before block or within the individual test itself. Once configured it will intercept any request to that specific URL and return the specified response. – Dan Reedy May 13 '13 at 14:34
  • 1
    Take a look at this presentation for more details http://www.slideshare.net/bartoszblimke/tdd-of-http-clients-with-webmock – Dan Reedy May 13 '13 at 14:38