vic*_*ich 4 rspec ruby-on-rails rspec-rails rspec3 ruby-on-rails-5
我有这个代码,我想在几个规范中重用:
RSpec.shared_context "a UserWorker" do |user|
let(:mock_context_user) {{
id: 1,
brand: user.brand,
backend_token: user.backend_token
}}
before(:each) do
allow(SomeClass).to receive(:some_method)
.with(user.id).and_return(mock_context_user)
end
before(:each, context: true) do
Sidekiq::Testing.inline!
end
after(:each, context: true) do
Sidekiq::Testing.fake!
end
end
Run Code Online (Sandbox Code Playgroud)
在使用共享代码的规范文件中:
let(:user) { build :user } # FactoryGirl
...
describe '#perform' do
# some lets here
include_context 'a UserWorker', user
context 'when something exists' do
it 'does some stuff' do
# test some stuff here
end
end
end
Run Code Online (Sandbox Code Playgroud)
但这给了我这个错误:
/.rvm/gems/ruby-2.3.0@fb-cont/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:724:in `method_missing': `user` is not available on an example group (e.g. a `describe` or `context` block). It is only available from within individual examples (e.g. `it` blocks) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). (RSpec::Core::ExampleGroup::WrongScopeError)
Run Code Online (Sandbox Code Playgroud)
建议?任何帮助表示赞赏。
该RSpec的文档都不会在这个很清楚,但你可以通过传递含块注入额外值let()来调用include_context。规范传递的“自定义块”将首先被评估,并且可用于在共享上下文中声明的代码。
这是一个共享上下文,它取决于规范,将let()其包含为一个值, value_from_spec,然后设置更多值,let()一个通过一个before()块,一个通过一个块:
RSpec.shared_context('a context', shared_context: :metadata) do
# assume the existence of value_from_spec
let(:a_value_from_context) { value_from_spec - 1 }
before(:each) do
# assume the existence of value_from_spec
@another_value_from_context = value_from_spec + 1
end
end
Run Code Online (Sandbox Code Playgroud)
(请注意,与 OP 的|user|示例不同,我们从未明确声明value_from_spec,我们只是相信它会在我们需要时出现。如果您想让正在发生的事情更加明显,您可以检查defined?(:value_from_spec)并引发错误。)
这是一个注入该值并读取它的共享上下文转换的规范:
describe 'passing values to shared context with let()' do
# "customization block"
include_context 'a context' do
# set value_from_spec here
let(:value_from_spec) { 1 }
end
describe 'the context' do
it 'should read the passed value in a let() block' do
expect(a_value_from_context).to eq(0)
end
it 'should read the passed value in a before() block' do
expect(@another_value_from_context).to eq(2)
end
end
end
Run Code Online (Sandbox Code Playgroud)
因为它总是返回相同的mock_context_user,你可以尝试一些更通用的东西,比如:
allow(SomeClass)
.to receive(:some_method)
.with(an_instance_of(Fixnum))
.and_return(mock_context_user)
Run Code Online (Sandbox Code Playgroud)
但我实际上不确定是否an_instance_of适用于 RSpec 3.5,它适用于 RSpec 3.3。
| 归档时间: |
|
| 查看次数: |
2359 次 |
| 最近记录: |