在 RSpec 2 中动态生成共享示例?
我试图通过创建一个共享示例组来保持我的规范干燥,该示例组对所有管理控制器(我项目的 Admin
命名空间下的所有控制器)执行样板检查。我正在努力弄清楚如何做到这一点,因为共享示例需要提供有关要使用的操作和参数的信息。理想情况下,如果测试失败,它应该呈现有意义的错误(即包括正在测试的操作的详细信息)。
require 'spec_helper'
shared_examples "an admin controller" do
before(:each) do
@non_admin = User.make
@admin = User.make(:admin)
end
context "as an admin user" do
@actions.each do |action, params|
specify "I should be able to access ##{action.last} via #{action.first}" do
self.active_user = @admin
send(action.first, action.last, params)
response.status.should be_ok
end
end
end
context "as a regular user" do
@actions.each do |action, params|
specify "I should be denied access to ##{action.last}" do
self.active_user = @non_admin
send(action.first, action.last, params)
response.status.should be 403
end
end
end
end
describe Admin::UserNotesController do
@user = User.make
@actions = { [:get, :index] => { :user_id => @user.id },
[:get, :new] => { :user_id => @user.id },
[:post, :create] => { :user_id => @user.id } }
it_behaves_like "an admin controller"
end
此错误的明显原因是 @actions
对共享示例组不可见。如果我使用 let
,则这只在示例的上下文中可用,而在 describe
块的上下文中不可用。有什么想法吗?
I'm trying to keep my specs DRY by creating a shared example group that performs the boilerplate checks for all admin controllers (all controllers under the Admin
namespace of my project). I'm struggling to figure out how to do it, since the shared example needs providing with the information about what actions and parameters to use. It should ideally present meaningful errors if a test fails (i.e. include the details of the action it was testing).
require 'spec_helper'
shared_examples "an admin controller" do
before(:each) do
@non_admin = User.make
@admin = User.make(:admin)
end
context "as an admin user" do
@actions.each do |action, params|
specify "I should be able to access ##{action.last} via #{action.first}" do
self.active_user = @admin
send(action.first, action.last, params)
response.status.should be_ok
end
end
end
context "as a regular user" do
@actions.each do |action, params|
specify "I should be denied access to ##{action.last}" do
self.active_user = @non_admin
send(action.first, action.last, params)
response.status.should be 403
end
end
end
end
describe Admin::UserNotesController do
@user = User.make
@actions = { [:get, :index] => { :user_id => @user.id },
[:get, :new] => { :user_id => @user.id },
[:post, :create] => { :user_id => @user.id } }
it_behaves_like "an admin controller"
end
This errors for the obvious reason that @actions
is not visible to the shared example group. If I use let
, this is only available in the context of an example, not in the context of the describe
block. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一种更简洁的方法:
请参阅 http:// relishapp.com/rspec/rspec-core/v/2-6/dir/example-groups/shared-examples 了解更多信息
Here's a much cleaner way that should work:
See http://relishapp.com/rspec/rspec-core/v/2-6/dir/example-groups/shared-examples for more information