运行2个测试时,Mock_redis无法正常工作,但在运行它们时会有效

时间:2018-01-23 15:08:32

标签: rspec redis ruby-on-rails-5.1

我有以下测试使用mock_redis来存根Redis安装。 (我使用的是Rails 5.1.4)

两个测试都希望通过使用2个不同的参数调用该方法,在这两种情况下都会将新密钥添加到Redis中。

describe '#paper_version' do  
  before do
    @redis_instance = MockRedis.new
    allow(Redis).to receive_message_chain(:new).and_return(@redis_instance)
  end

  it 'stores the reduced version on Redis Cache when paper_type == reduced ' do
    foo.paper_version('reduced')
    expect(@redis_instance.keys).to include "reduced_paper_#{foo.id}"
  end
  it 'stores the full version on Redis Cache when paper_type == full ' do
    foo.paper_version('full')
    expect(@redis_instance.keys).to include "full_paper_#{foo.id}"
  end
end

如果我只运行其中一个,一切正常并且测试通过没有问题,但如果我同时运行,第二个总是失败并出现以下错误:

Failure/Error: expect(@redis_instance.keys).to include "full_paper_#{foo.id}"
   expected [] to include "full_paper_97"

为什么会发生这种情况的任何想法?

我的gemlist:

rails (5.1.4)
rspec-core (3.7.1)
rspec-expectations (3.7.0)
rspec-mocks (3.7.0)
rspec-rails (3.7.2)
rspec-support (3.7.0)
mock_redis (0.17.3)

0 个答案:

没有答案