英文:
Rspec how to determine if a let block has been defined?
问题
在Rspec中,我想要利用super()
来调用一个已定义的let
块(如果存在的话),或者在不存在时设置一个新的值,我想在一个shared_example
组内使用这个功能,但我找不到如何实现这个目标。
我尝试过检查@some_let
是否存在,尝试过检查some_let
的超级方法是否由kernel
拥有,但都没有提供有用的信息;我无法访问instance_methods
或instance_method
,因为Rspec不允许我这样做,而在互联网上搜索方法也没有找到答案。
我希望能够像这样做:
shared_examples 'a shared example' do
let(:some_let) { let_exists?(:some_let) ? super() : some_new_value }
end
是否有类似let_exists?
的方法或类似的方法?
英文:
In Rspec, I want to take advantage of using super()
to call a defined let
block if it exists or set a new value if it hasn't, I want to use this within a shared_example
group but I just can't find how to do this.
I've tried checking if @some_let
exists, I've tried checking if a super method of :some_let
is owned by the kernel
or not and none of them provide anything useful; I can't access instance_methods
or instance_method
because Rspec won't let me and searching the internet for a method hasn't revealed an answer.
I want to be able to do something like this:
shared_examples 'a shared example' do
let(:some_let) { let_exists?(:some_let) ? super() : some_new_value }
end
is there a method like let_exists?
or something to that effect?
答案1
得分: 7
假设在包含共享示例之前调用了 let
,则以下内容将起作用:
shared_examples 'a shared example' do
let(:some) { 'fallback value' } unless method_defined?(:some)
end
describe 'with let' do
let(:some) { 'explicit value' }
include_examples 'a shared example'
it { expect(some).to eq('explicit value') }
end
describe 'without let' do
include_examples 'a shared example'
it { expect(some).to eq('fallback value') }
end
method_defined?
检查当前上下文中是否已经定义了名为 some
的方法。如果没有定义,则定义该方法以提供默认值。
另一种(通常更简单的)方法是始终定义一个默认值,并在包含共享示例后提供显式值(从而覆盖默认值):
shared_examples 'a shared example' do
let(:some) { 'default value' }
end
describe 'with let' do
include_examples 'a shared example' # <- 顺序很重要
let(:some) { 'explicit value' } # important
it { expect(some).to eq('explicit value') }
end
describe 'without let' do
include_examples 'a shared example'
it { expect(some).to eq('default value') }
end
英文:
Assuming that you call let
before including the shared examples, this would work:
shared_examples 'a shared example' do
let(:some) { 'fallback value' } unless method_defined?(:some)
end
describe 'with let' do
let(:some) { 'explicit value' }
include_examples 'a shared example'
it { expect(some).to eq('explicit value') }
end
describe 'without let' do
include_examples 'a shared example'
it { expect(some).to eq('fallback value') }
end
method_defined?
checks if a method called some
has already been defined in the current context. If not, the method is defined to provide a default value.
Another (usually easier) approach is to always define a default value and to provide the explicit value after including the shared examples: (thus overwriting the default value)
shared_examples 'a shared example' do
let(:some) { 'default value' }
end
describe 'with let' do
include_examples 'a shared example' # <- order is
let(:some) { 'explicit value' } # important
it { expect(some).to eq('explicit value') }
end
describe 'without let' do
include_examples 'a shared example'
it { expect(some).to eq('default value') }
end
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论