Rspec 当响应类型为'时,如何测试响应类型为';在shoulda_matches 2.0中不推荐使用
Rspec 当响应类型为'时,如何测试响应类型为';在shoulda_matches 2.0中不推荐使用,rspec,rspec2,rspec-rails,Rspec,Rspec2,Rspec Rails,respond\u with\u content\u typematcher现在在gem中被弃用(版本>2.0,版本1.5.6中有很多丑陋的警告) 虽然Thoughtbot建议开发人员应该使用集成测试,但这并不总是在资源不足的项目上 所以问题是如何修复损坏的规格。。。或者如何替换它们 指: 最简单的方法是将出现的respond\u替换为\u content\u type: # spec/controllers/users_controller_spec.rb describe Users
respond\u with\u content\u type
matcher现在在gem中被弃用(版本>2.0,版本1.5.6中有很多丑陋的警告)
虽然Thoughtbot建议开发人员应该使用集成测试,但这并不总是在资源不足的项目上
所以问题是如何修复损坏的规格。。。或者如何替换它们
指:
respond\u替换为\u content\u type
:
# 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}
...
B.T.W.:如果你错过了matcher
应该将_分配给
,那么就有了一个现有的解决方案。Gem只是一个简单的shoulda matcher扩展模块最简单的方法是用内容类型替换任何出现的响应类型
:
# 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}
...
B.T.W.:如果你错过了matcher应该将_分配给
,那么就有了一个现有的解决方案。Gem只是一个简单的shoulda matcher扩展模块