除了尝试运行单个it块之外,我有一个没有问题的规范。在本例中,我得到了一个Failure/Error: before(:context),并给出了解释:
不支持在每个测试生命周期之外使用来自rspec-模拟的双倍或部分双倍。不建议使用rspec-
:should的旧:should语法,而不显式启用语法。使用新的:expect语法或显式启用:should。
问题是我不使用rspec-mocks stub方法,而是使用dry-container定义的方法
就像这样:
require 'dry/container/stub'
before { FooContainer.enable_stubs! }
before(:context) { FooContainer.stub 'foo.key', stubbed_operation }
after(:context) { FooContainer.unstub 'foo.key' }有没有一种方法可以在不启用旧的RSpec语法的情况下禁用此rspec-mocks行为?
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)发布于 2019-04-19 16:54:43
我认为问题在于,您启用了before(:each)块中的存根,而不是before(:context)块中的存根,这是在before(:each)块之前导出的。此时,rspec/ruby不知道来自dry-container的dry-container方法,因此它尝试使用rspec-mock中的默认stub方法。
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 来自干式容器测试文件
# before stub you need to enable stubs for specific container
container.enable_stubs!
container.stub(:redis, "Stubbed redis instance")发布于 2019-04-18 00:25:20
我现在找到了一个解决办法如果我使用:
before { FooContainer.stub 'foo.key', stubbed_operation }
after { FooContainer.unstub 'foo.key' }而不是:
before(:context) { FooContainer.stub 'foo.key', stubbed_operation }
after(:context) { FooContainer.unstub 'foo.key' }它起作用了。我能看到的唯一缺点是,它将花费一点性能,而且它可能会在未来崩溃。
https://stackoverflow.com/questions/55736828
复制相似问题