Refactor test to stop using .any_instance

Although might be useful in very particular cases its use is discourage
by RSpec itself. See https://relishapp.com/rspec/rspec-mocks/docs/working-with-legacy-code/any-instance
This commit is contained in:
Pau Perez
2019-03-06 16:07:22 +01:00
parent bc34d04c31
commit b9636b975a

View File

@@ -6,11 +6,13 @@ describe RefreshProductsCacheJob do
let(:order_cycle) { create(:simple_order_cycle) }
context 'when the enterprise and the order cycle exist' do
it "renders products and writes them to cache" do
RefreshProductsCacheJob.any_instance.stub(:products_json) { 'products' }
before do
refresh_products_cache_job = instance_double(OpenFoodNetwork::ProductsRenderer, products_json: 'products')
allow(OpenFoodNetwork::ProductsRenderer).to receive(:new).with(distributor, order_cycle) { refresh_products_cache_job }
end
it 'renders products and writes them to cache' do
run_job RefreshProductsCacheJob.new distributor.id, order_cycle.id
expect(Rails.cache.read("products-json-#{distributor.id}-#{order_cycle.id}")).to eq 'products'
end
end