title: Testing XmlBuilder with Rspec
body: <p>I posted a question (<a href="https://stackoverflow.com/questions/30078753/rails-xml-builder-not-rendering/30079026">Rails XML builder not rendering</a>) regarding rendering XML using XmlBuilder. That particular issue was resolved but I’ve run into another issue testing this controller action.</p>
  
  <p>The issue seems to be with Rspec as because the same test written in Minitest works fine and using cURL works too.</p>
  
  <p>While debugging this, Rspec never tries to render the xml builder template. It just skips over the template and renders a http status 200 with a blank body.</p>
  
  <p>Here are the controller, xml builder, tests, and test results:</p>
  
  <p>controllers/bars_controller.rb</p>
  
  <pre><code>require 'builder'
  
  class BarsController &lt; ApplicationController
  
    def show
      @bar = Bar.find(params[:id])
      render template: 'bars/show.xml.builder', formats: [:xml]
    end
  
   end
  </code></pre>
  
  <p>/views/bars/show.xml.builder</p>
  
  <pre><code>xml.instruct!
  xml.bar do
    xml.foo(@bar.foo)
    xml.bar(@bar.bar)
  end
  </code></pre>
  
  <p>/test/controllers/bars_controller_test.rb</p>
  
  <pre><code> require 'test_helper'
  
   class BarsControllerTest &lt; ActionController::TestCase
    setup do
      @bar = Bar.create(foo: 'bar', bar: 'foo')
    end
  
    test "should show bar" do
      get :show, id: @bar
      assert_response :success
      assert_match "&lt;bar&gt;", response.body
    end
   end
  </code></pre>
  
  <p>/spec/controllers/bars_controller_spec.rb</p>
  
  <pre><code> require_relative '../rails_helper'
  
   describe BarsController do
  
    describe 'a POST to :show' do
      before do
        @bar = Bar.create(foo: 'bar', bar: 'foo')
        post :show, id: @bar
      end
  
      it 'should show bar' do
        expect(response).to be_success
        expect(response.body).to include("&lt;bar&gt;")
      end
    end
   end
  </code></pre>
  
  <h2>Test results</h2>
  
  <pre><code>&gt; rake test
  Run options: --seed 50688
  
  # Running:
  .
  Finished in 0.096901s, 10.3198 runs/s, 30.9593 assertions/s.
  1 runs, 3 assertions, 0 failures, 0 errors, 0 skips
  
  &gt; rspec spec/controllers/bars_controller_spec.rb 
  F
  
  Failures:
  
    1) BarsController a POST to :show responds with xml bar
       Failure/Error: expect(response.body).to include("&lt;bar&gt;")
         expected "" to include "&lt;bar&gt;"
       # ./spec/controllers/bars_controller_spec.rb:13:in `block (3 levels) in &lt;top (required)&gt;'
  
  Finished in 0.01726 seconds (files took 1.53 seconds to load)
  1 example, 1 failure
  
  Failed examples:
  
  rspec ./spec/controllers/bars_controller_spec.rb:11 # BarsController a POST to :show responds with xml bar
  </code></pre>