<p>Given a controller method like: </p>

<pre><code>def show
  @model = Model.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml =&gt; model }
  end
end
</code></pre>

<p>What's the best way to write an integration test that asserts that the return has the expected XML?</p>

[{'answer_id': 59714, 'author': 'Michael Sepcot', 'author_id': 6033, 'author_profile': 'https://Stackoverflow.com/users/6033', 'pm_score': 0, 'selected': False, 'text': "<p>Set the request objects accept header:</p>\n\n<pre><code>@request.accept = 'text/xml' # or 'application/xml' I forget which\n</code></pre>\n\n<p>Then you can assert the response body is equal to what you were expecting</p>\n\n<pre><code>assert_equal '&lt;some&gt;xml&lt;/some&gt;', @response.body\n</code></pre>\n"}, {'answer_id': 60261, 'author': 'btandyco', 'author_id': 6189, 'author_profile': 'https://Stackoverflow.com/users/6189', 'pm_score': 3, 'selected': False, 'text': "<p>This is the idiomatic way of testing the xml response from a controller.</p>\n\n<pre><code>class ProductsControllerTest &lt; ActionController::TestCase\n  def test_should_get_index_formatted_for_xml\n    @request.env['HTTP_ACCEPT'] = 'application/xml'\n    get :index\n    assert_response :success\n  end\nend\n</code></pre>\n"}, {'answer_id': 60626, 'author': 'ntalbott', 'author_id': 6284, 'author_profile': 'https://Stackoverflow.com/users/6284', 'pm_score': 5, 'selected': True, 'text': '<p>A combination of using the format and assert_select in an integration test works great:</p>\n\n<pre><code>class ProductsTest &lt; ActionController::IntegrationTest\n  def test_contents_of_xml\n    get \'/index/1.xml\'\n    assert_select \'product name\', /widget/\n  end\nend\n</code></pre>\n\n<p>For more details check out <a href="http://apidock.com/rails/ActionController/Assertions/SelectorAssertions/assert_select" rel="noreferrer">assert_select</a> in the Rails docs.</p>\n'}, {'answer_id': 613309, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>These 2 answers are great, except that my results include the datetime fields, which are gong to be different in most circumstances, so the <code>assert_equal</code> fails.  It appears that I will need to process the include <code>@response.body</code> using an XML parser, and then compare the individual fields, the number of elements, etc.  Or is there an easier way?</p>\n'}, {'answer_id': 3532203, 'author': 'bjnord', 'author_id': 291754, 'author_profile': 'https://Stackoverflow.com/users/291754', 'pm_score': 3, 'selected': False, 'text': "<p>The answer from ntalbott shows a get action. The post action is a little trickier; if you want to send the new object as an XML message, and have the XML attributes show up in the params hash in the controller, you have to get the headers right. Here's an example (Rails 2.3.x):</p>\n\n<pre><code>class TruckTest &lt; ActionController::IntegrationTest\n  def test_new_truck\n    paint_color = 'blue'\n    fuzzy_dice_count = 2\n    truck = Truck.new({:paint_color =&gt; paint_color, :fuzzy_dice_count =&gt; fuzzy_dice_count})\n    @headers ||= {}\n    @headers['HTTP_ACCEPT'] = @headers['CONTENT_TYPE'] = 'application/xml'\n    post '/trucks.xml', truck.to_xml, @headers\n    #puts @response.body\n    assert_select 'truck&gt;paint_color', paint_color\n    assert_select 'truck&gt;fuzzy_dice_count', fuzzy_dice_count.to_s\n  end\nend\n</code></pre>\n\n<p>You can see here that the 2nd argument to post doesn't have to be a parameters hash; it can be a string (containing XML), <strong>if</strong> the headers are right. The 3rd argument, @headers, is the part that took me a lot of research to figure out.</p>\n\n<p>(Note also the use of to_s when comparing an integer value in assert_select.)</p>\n"}]
['https://Stackoverflow.com/questions/59655', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4748/']