Ruby on rails 在RSPEC2中动态生成共享示例?
我试图通过创建一个共享示例组来保持我的规范不变,该示例组对所有管理员控制器(我的项目的Ruby on rails 在RSPEC2中动态生成共享示例?,ruby-on-rails,ruby,rspec,rspec2,Ruby On Rails,Ruby,Rspec,Rspec2,我试图通过创建一个共享示例组来保持我的规范不变,该示例组对所有管理员控制器(我的项目的admin命名空间下的所有控制器)执行样板检查。我正在努力弄清楚如何做到这一点,因为共享示例需要提供有关要使用哪些操作和参数的信息。理想情况下,如果测试失败,它应该显示有意义的错误(即,包括它正在测试的操作的细节) 此错误的明显原因是@actions对共享示例组不可见。如果我使用let,这仅在示例的上下文中可用,而在descripe块的上下文中不可用。有什么想法吗?这里有一个更干净的方法应该可以: requir
admin
命名空间下的所有控制器)执行样板检查。我正在努力弄清楚如何做到这一点,因为共享示例需要提供有关要使用哪些操作和参数的信息。理想情况下,如果测试失败,它应该显示有意义的错误(即,包括它正在测试的操作的细节)
此错误的明显原因是
@actions
对共享示例组不可见。如果我使用let
,这仅在示例的上下文中可用,而在descripe
块的上下文中不可用。有什么想法吗?这里有一个更干净的方法应该可以:
require 'spec_helper'
shared_examples "an admin controller" do |actions|
context "as an admin user" do
actions.each_pair do |action, verb|
specify "I should be able to access ##{action} via #{verb}" do
send(verb, action, :user_id => User.make(:admin).id)
response.status.should be_ok
end
end
end
context "as a regular user" do
actions.each_pair do |action, verb|
specify "I should be denied access to ##{action}" do
send(verb, action, :user_id => User.make.id)
response.status.should be 403
end
end
end
end
describe Admin::UserNotesController do
it_behaves_like "an admin controller", {
:index => :get,
:new => :get,
:create => :post
}
end
有关更多信息,请参见这太棒了,谢谢!没有什么比删除代码更好的了:)
require 'spec_helper'
shared_examples "an admin controller" do |actions|
context "as an admin user" do
actions.each_pair do |action, verb|
specify "I should be able to access ##{action} via #{verb}" do
send(verb, action, :user_id => User.make(:admin).id)
response.status.should be_ok
end
end
end
context "as a regular user" do
actions.each_pair do |action, verb|
specify "I should be denied access to ##{action}" do
send(verb, action, :user_id => User.make.id)
response.status.should be 403
end
end
end
end
describe Admin::UserNotesController do
it_behaves_like "an admin controller", {
:index => :get,
:new => :get,
:create => :post
}
end