默认情况下,Rails测试中的设置请求格式被覆盖

时间:2015-11-13 10:24:37

标签: ruby-on-rails unit-testing format rails-routing

我的namespace中定义了config/routes.rb,它将控制器请求的默认格式设置为json。我想编写一个测试,使用不同的格式向该控制器发出请求,但它似乎不起作用。

配置/ routes.rb中

Rails.application.routes.draw do
  namespace :foo, defaults: { format: :json } do
    resources :bars
  end
end

应用/控制器/富/ bars_controller.rb

class Foo::BarsController < ActionController::API
  before_action :print_request_format

  def print_request_format
    puts "Request format is #{request.format}"
  end
end

测试/集成/ format_test.rb

class FormatTest < ActionDispatch::IntegrationTest

  test "XML request" do
    get foo_bars_path, format: "xml"
    assert_match /application\/xml/, request.format

    # Assert other stuff for handling XML.
  end
end

当我运行测试时,断言失败并且输出为Request format is application/json,因此format: "xml"被路由文件中的默认值忽略或覆盖。有什么方法可以在测试中指定格式而不是覆盖它?

0 个答案:

没有答案
相关问题