【问题标题】:RSpec 3 - deprecated "Should"RSpec 3 - 弃用“应该”
【发布时间】:2019-03-01 16:16:51
【问题描述】:

如果不推荐使用should,我不想启用它的使用。新的expect 语法是什么?

   describe '#show response' do 
        it "should return html data only" do 
          get :show, params: {:id => "bike"}
          response.header['Content-Type'].should include 'text/html'
        end

        it "should not return json data" do 
          get :show, params: {:id => "bike"}
          response.header['Content-Type'].should_not include 'application/json'
        end

        it "should not return js data" do 
          get :show, params: {:id => "bike"}
          response.header['Content-Type'].should_not include 'text/javascript'
        end
      end
    end

弃用警告:

使用来自 rspec-expectations 的旧 :should 语法的 should 显式启用语法已被弃用。使用新的:expect 语法或使用config.expect_with(:rspec) { |c| c.syntax = :should } 显式启用:should

【问题讨论】:

  • Rspec 有很棒的文档,并且期望语法已经成为默认语法已经有一段时间了。例如,您正在寻找 include matcher
  • RSpec 3,而不是 Rails!,鼓励用户使用新的 expect 语法 - 如博客文章中所述。 RSpec 3 会在首次使用任何旧语法方法时打印警告 ... 除非明确启用了 should 语法。 rspec.info/blog/2013/07/the-plan-for-rspec-3

标签: ruby-on-rails ruby-on-rails-5 rspec-rails


【解决方案1】:

您可以考虑使用expect().to eq(),所以在您的情况下:

expect(response.header['Content-Type'].include?('text/html')).to be true
expect(response.header['Content-Type'].include?('application/json')).to be false
expect(response.header['Content-Type'].include?('text/javascript')).to be false

更多关于rspec匹配器的信息在这里:https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers

【讨论】:

  • 它实际上更像expect(response.header['Content-Type']).to include( 'text/html')`,显然倒置是not_to include
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多