Rspec:弃用警告

时间:2017-04-24 21:56:37

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

我创建了一个以下的rspec模拟运行正常但我在执行时收到警告。

Using `stub` from rspec-mocks' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` instead. called from /file/path.rb:26:in `block (3 levels) in <top (required)>'.

这是我的单元测试。在我的单元测试中,第26行是user_health_condition.stub(:user_condition_flag) do |user_id|。每次警告我都在期待,为什么我会收到这个警告?

describe '.user_condition_flag' do
  let(:expected_result_with_diabetes) { 'Y' }
  let(:expected_result_without_diabetes) { 'N' }
  let(:user_id_1) { 38 }
  let(:user_id_2) { 39 }

  context 'with entries in table' do
    it 'returns expected results' do
      user_health_condition = double(user_health_condition)
      allow(user_health_condition).to receive(:user_condition_flag).and_return(expected_result_with_diabetes)
      user_health_condition.stub(:user_condition_flag) do |user_id|
        if user_id == :user_id_1
          'Y'
        elsif user_id == :user_id_2
          'N'
        end
      end

      expect(user_health_condition.user_condition_flag(:user_id_1)).to eq(expected_result_with_diabetes)
      expect(user_health_condition.user_condition_flag(:user_id_2)).to eq(expected_result_without_diabetes)
    end
  end
end

2 个答案:

答案 0 :(得分:4)

详细说明@Brad的答案,以防link死于某一点

  

allow(User).to receive(:find_by).and_return(user)代替:

     

User.stub(:find_by).and_return(user)

替换

user_health_condition.stub(:user_condition_flag) do

使用

allow(user_health_condition).to receive(:user_condition_flag) do

答案 1 :(得分:1)