# Post\_via\_redirect - testing problem

**URL:** <https://www.ruby-forum.com/t/post-via-redirect-testing-problem/118417>\
**Category:** Rails\
**Created:** [October 26, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/post-via-redirect-testing-problem/118417 "2007-10-26T18:57:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 26, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/post-via-redirect-testing-problem/118417/1 "2007-10-26T18:57:54Z")

</div>

I’ve got an integration test that’s failing but i can’t work out why:

Here’s the action being tested - in case anyone recognises it, it’s  
based on the Depot app in Agile Web Dev with Rails.

def checkout  
@in\_checkout = true  
#don’t let user checkout with an empty cart  
if @cart.items.empty?  
redirect\_to\_index(“Your cart is empty”)  
else  
#see if the form is being submitted, ie if it’s a POST request  
if request.post?  
#make a new object with the form data  
@order = Order.create(:name =\> params[:name],  
:address =\> params[:address],  
:email =\> params[:email],  
:pay\_type =\> params[:pay\_type])  
@order.add\_line\_items\_from\_cart(@cart)  
#if we saved the order, empty the cart and go back to index page  
if @order.save  
session[:cart] = nil  
redirect\_to\_index(“Thank you for your order”)  
else  
#if it didn’t save, show the order form again  
render :action =\> :checkout  
end  
else#not a POST request, ie a GET request detected. Should be  
case for  
#when an empty form is displayed.  
#make a new order object for the form to work with  
@order = Order.new  
end#if request.post?  
end  
end

So, the checkout action can be called with a get (to create the empty  
form) or a post (to send the details to make a new Order object)  
request. In my testing, the get request works fine but the post request  
seems to not create the order object correctly: here’s the problem part  
of the test:

```
post_via_redirect "/store/checkout",
            { :name => "Dave T.",
              :address => "123 The Street",
              :email => "removed_email_address@domain.invalid",
              :pay_type => "check"
            }

assert_response :success
assert_template "index"
assert_equal nil, session[:cart]

```

So, i’m passing the relevant details to checkout as part of a post  
request - as far as i can tell this is what happens when i hit the  
“place order” button, using the app normally. But it looks like the  
action’s going wrong somewhere when i use the test.

Can anyone see what i’m doing wrong?  
thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 26, 2007, 7:13pm UTC](https://www.ruby-forum.com/t/post-via-redirect-testing-problem/118417/2 "2007-10-26T19:13:52Z")

</div>

Just to give more info, it gets even more puzzling when i debug it:  
stepping through the controller code, at the point where it gets to the  
if statement that tests the request type:

if request.post?

At this point, i’ve sent a post\_via\_redirect request, and according to  
the debugger, self.request.request\_method = “post”. BUT, the if test  
returns false, and sends me into the else section (which is why the  
integration test is failing).

Can anyone explain this to me? It seems crazy.
