Reputation: 5273
I have a spec that runs without issues except when I try to run single it
blocks. In this case I get a Failure/Error: before(:context)
with the explanation:
The use of doubles or partial doubles from rspec-mocks outside of the per-test lifecycle is not supported. 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.
The issue is that I don't use the rspec-mocks
stub
method but the one defined by dry-container
:
Like so:
require 'dry/container/stub'
before { FooContainer.enable_stubs! }
before(:context) { FooContainer.stub 'foo.key', stubbed_operation }
after(:context) { FooContainer.unstub 'foo.key' }
Is there a way to disable this RSpec behaviour without enabling the old rspec-mocks
syntax?
rspec --version
RSpec 3.8
- rspec-core 3.8.0
- rspec-expectations 3.8.2
- rspec-mocks 3.8.0
- rspec-rails 3.8.2
- rspec-support 3.8.0
rails -v
Rails 5.2.2.1
ruby -v
ruby 2.6.2p47 (2019-03-13 revision 67232) [x86_64-linux]
dry-container (0.6.0)
Upvotes: 1
Views: 1392
Reputation: 717
I think the problem is, that you enabled the stub in the before(:each)
block not within the before(:context)
block, which is execuded before the before(:each)
block. At this point the stub
method from dry-container
is unknown to rspec/ruby and therefore it tries to use the default stub
method from rspec-mock
.
require 'dry/container/stub'
before(:context) { FooContainer.enable_stubs! }
before(:context) { FooContainer.stub 'foo.key', stubbed_operation }
# or better
before(:context) do
FooContainer.enable_stubs!
FooContainer.stub 'foo.key', stubbed_operation
end
after(:context) { FooContainer.unstub 'foo.key' }
context "my context" do
it "my test" do
...
end
end
From the dry-container testing documentation
# before stub you need to enable stubs for specific container
container.enable_stubs!
container.stub(:redis, "Stubbed redis instance")
Upvotes: 1
Reputation: 5273
I've found a workaround now if I use:
before { FooContainer.stub 'foo.key', stubbed_operation }
after { FooContainer.unstub 'foo.key' }
instead of:
before(:context) { FooContainer.stub 'foo.key', stubbed_operation }
after(:context) { FooContainer.unstub 'foo.key' }
It works. The only drawback I can see is that it will cost a little performance and that it could break in the future.
Upvotes: 1