1

respond_with_content_typematcher 现在在shoulda-matchers gem 中已弃用(版本 > 2.0 并且版本 1.5.6 中有很多丑陋的警告)

Thoughtbot 建议开发人员应该使用集成测试,但这并不总是在低资源项目上

所以问题是如何修复损坏的规格?...或如何更换它们

参考:

4

1 回答 1

1

最简单的方法是替换任何出现的respond_with_content_typewith :

# spec/controllers/users_controller_spec.rb
describe UsersController do
  before{ get :index, :format => :xlsx }
  it 'response should be excel format' do
    response.content_type.to_s.should eq Mime::Type.lookup_by_extension(:xlsx).to_s
  end
end

如果你想要一个合适的匹配器:

# spec/support/matchers/respond_with_content_type_matchers.rb
RSpec::Matchers.define :respond_with_content_type do |ability|
  match do |controller|
    expected.each do |format|  # for some reason formats are in array
      controller.response.content_type.to_s.should eq Mime::Type.lookup_by_extension(format.to_sym).to_s
    end
  end

  failure_message_for_should do |actual|
    "expected response with content type #{actual.to_sym}"
  end

  failure_message_for_should_not do |actual|
    "expected response not to be with content type #{actual.to_sym}"
  end
end

# spec/spec_helper.rb
...
#ensure support dir is loaded
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}  
...

顺便说一句:如果你错过了匹配器should assign_to,那么现有的解决方案https://github.com/tinfoil/should-kept-assign-to。Gem 只是一个简单的 shoulda-matcher 扩展模块

于 2013-09-12T09:19:29.607 回答