XmlBuilder を使用した XML のレンダリングに関する質問 ( Rails XML ビルダーがレンダリングしない) を投稿しました。その特定の問題は解決されましたが、このコントローラー アクションのテスト中に別の問題が発生しました。
Minitest で記述された同じテストが正常に機能し、cURL の使用も機能するため、問題は Rspec にあるようです。
これをデバッグしている間、Rspec は決して xml ビルダー テンプレートをレンダリングしようとしません。テンプレートをスキップして、本文が空白の http ステータス 200 をレンダリングするだけです。
コントローラー、xml ビルダー、テスト、およびテスト結果は次のとおりです。
コントローラー/bars_controller.rb
require 'builder'
class BarsController < ApplicationController
def show
@bar = Bar.find(params[:id])
render template: 'bars/show.xml.builder', formats: [:xml]
end
end
/views/bars/show.xml.builder
xml.instruct!
xml.bar do
xml.foo(@bar.foo)
xml.bar(@bar.bar)
end
/test/controllers/bars_controller_test.rb
require 'test_helper'
class BarsControllerTest < 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 "<bar>", response.body
end
end
/spec/controllers/bars_controller_spec.rb
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("<bar>")
end
end
end
試験結果
> 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
> 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("<bar>")
expected "" to include "<bar>"
# ./spec/controllers/bars_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
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