如何在RSPEC中使用相同名称的变量传递?
让我们想象我有一些测试,想使用共享的示例,例如:
RSpec.shared_examples "shared examples" do
let(:x) { "" }
it "should be equal to 5" do
expect(x).to eq(5)
end
end
然后使用它:
describe "my tests" do
let(:x) { 5 }
it_behaves_like "shared examples" do
let(:x) { x }
end
end
我知道我可以隐式地做到这一点,而无需传递let(:x){x){x}
块,一切都会起作用。但是我要实现的目标是为我的测试增加更加清晰度。
因此,问题是:如何通过相同名称传递变量(如果需要的话),而不会陷入最大呼叫堆栈错误到子共享示例块中?
请让我知道我的方法一般不正确。
Let's imagine I have some tests and want to use shared examples, e.g.:
RSpec.shared_examples "shared examples" do
let(:x) { "" }
it "should be equal to 5" do
expect(x).to eq(5)
end
end
and then use it like:
describe "my tests" do
let(:x) { 5 }
it_behaves_like "shared examples" do
let(:x) { x }
end
end
I know that I can do that implicitly, without passing let(:x) { x }
to the child block and everything will work. But what I'm trying to achieve - is to add more clarity to my tests.
So the question is: how to pass a variable (override if you want) with the same name without falling into maximum call stack error to a child shared examples block?
Please let me know if my approach is not right in general.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需要定义变量内部块,
let
是懒惰的,因此直到第一次才能对其进行评估它定义的方法是被调用的,但是它们需要在
上下文
之类的内部块中,类似的内容:You just need to define the variable inside blocks,
let
is lazy-evaluated, so it is not evaluated until the first timethe method it defines is invoked, but they need to be inside blocks like
context
, something like this: