views:

701

answers:

2

I am trying to sending XML content through POST to a controller ('Parse') method ('index') in a simple Rails project. It is not RESTful as my model name is different, say, 'cars'. I have the following in a functional test that works:

def test_index
   ...
   data_file_path = File.dirname(__FILE__) + 
        '/../../app/views/layouts/index.xml.erb'

   message = ERB.new( File.read( data_file_path ) )
   xml_result = message.result( binding )
   doc = REXML::Document.new xml_result

   @request.env['RAW_POST_DATA'] = xml_result
   post :index
   assert_response :success
end

I am now trying cucumber (0.4.3), and would like to know as to how I can simulate the POST request in a "When" clause. I have only one controller method 'index', and I have the following in config/routes.rb:

ActionController::Routing::Routes.draw do |map|
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end
  1. webrat within cucumber is only for HTML, and cannot do POST?
  2. @request variable is not available from cucumber environment?
  3. If I use something like 'visit index' (assuming it is Parse controller, index method) in features/step_definitions/car_steps.rb, I get the following error:

undefined method `index' for # (NoMethodError)

Appreciate any suggestions on how to do integration tests with Cucumber for HTTP POST with XML content.

+3  A: 

Webrat won't help you here, it's for browser based interactions so if you are specing an API it won't help.

You can use 'post' in Cucumber but you need to provide the full path to the action, not just the action. Also, pass in the Content-type header so Rails knows you are passing in XML.

post("/controller/index", xml_result, {"Content-type" => "text/xml"})

On the response side you can do the following:

response.should be_success
Patrick Ritchie
Bingo! Thanks for your help.
mbuf
mbuf: if this answered your question could you please accept the answer? That way other stackoverflow users will be able to tell without having to read the comments.
Patrick Ritchie
Accepted. Thanks for your response!
mbuf
+2  A: 

Patrick Ritchie's solution helped me out too, but I needed to make a slight modification to make it work with Rails 3.

post("/controller/index", xml_result, {"CONTENT_TYPE" => "text/xml"})

I think this is because, in v3, Rails is more tightly integrated with Rack.

Louis Rose