【发布时间】:2018-05-29 16:28:38
【问题描述】:
我正在重构我的测试。我心想,如果正确的用户登录,我应该将负责处理的逻辑和我的成功行为的逻辑分开。所以我开始用我不成功的上下文创建一个共享示例,但我被困在了这一刻:
RSpec.shared_examples "changing status" do |arguments|
/* ... */
it_behaves_like "action requiring proper user logged in to be successful", action
context "- when the required logged user is logged in" do
before(:each) do
/* ... */
end
context "and when the reservation has an allowed status" do
/* ... */
end
context "and when the reservation has a not allowed status" do
/* ... */
end
end
end
RSpec.shared_examples "action requiring proper user logged in to be successful" do |action|
context "- when the logged in user is not the required logged user" do
before(:each) do
login(incidental_user)
end
it_behaves_like "unsuccessful attempt to change the reservation", action
end
context "- when there's no user logged in" do
it_behaves_like "unsuccessful attempt to change the reservation", action
end
end
所以,我想将代码从我的上下文 "when the required logged user is logged in" 注入到我的共享示例中以使其干净。我尝试使用匿名代码块和yield 关键字:
RSpec.shared_examples "changing status" do |arguments|
/* ... */
it_behaves_like "action requiring proper user logged in to be successful", action do
before(:each) do
/* ... */
end
context "and when the reservation has an allowed status" do
/* ... */
end
context "and when the reservation has a not allowed status" do
/* ... */
end
end
end
RSpec.shared_examples "action requiring proper user logged in to be successful" do |action|
context "- when the required logged user is logged in" do
yield
end
context "- when the logged in user is not the required logged user" do
before(:each) do
login(incidental_user)
end
it_behaves_like "unsuccessful attempt to change the reservation", action
end
context "- when there's no user logged in" do
it_behaves_like "unsuccessful attempt to change the reservation", action
end
end
但不幸的是它得到了这个错误:
LocalJumpError: 没有给出块(产量)
那么,我该怎么做呢?
【问题讨论】:
标签: ruby-on-rails dependency-injection rspec