【发布时间】:2016-10-09 19:19:24
【问题描述】:
我在为 RSpec 中的共享示例使用已定义变量时遇到问题。这是我的测试:
RSpec.shared_examples "check user logged in" do |method, action, params|
it "redirects to the sign in page if the user is not logged in" do
send(method, action, params)
expect(response).to redirect_to(signin_url)
end
end
RSpec.describe UsersController, type: :controller do
describe "GET #show" do
let(:user) { FactoryGirl.create(:user) }
let!(:show_params) do
return { id: user.id }
end
context "navigation" do
include_examples "check user logged in", :get, :show, show_params
end
end
end
在测试中,我正在检查以确保用户需要登录才能执行操作。我收到以下错误消息:
method_missing':show_params 在示例组中不可用
我需要进行哪些更改才能使 show_params 可访问?我试过使用it_behaves_like 而不是include_examples,但没有成功。我也尝试删除 context "navigation" 块无济于事。我需要跨多个控制器和操作执行此检查,因此似乎共享示例可能是重用代码的正确方法。
【问题讨论】:
标签: ruby-on-rails testing rspec