Rspec: 废弃警告

8

我创建了一个 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|。我使用 expect,但是为什么还会收到警告?

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
1个回答

12
为了防止link失效,进一步解释@Brad的答案:

使用allow(User).to receive(:find_by).and_return(user)替代以下代码:

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

请进行替换。
user_health_condition.stub(:user_condition_flag) do

With

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接