在 RSpec 2 中动态生成共享示例?

发布于 2024-11-09 22:50:47 字数 1380 浏览 0 评论 0原文

我试图通过创建一个共享示例组来保持我的规范干燥,该示例组对所有管理控制器(我项目的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执笏见 2024-11-16 22:50:47

这是一种更简洁的方法:

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

请参阅 http:// relishapp.com/rspec/rspec-core/v/2-6/dir/example-groups/shared-examples 了解更多信息

Here's a much cleaner way that should work:

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

See http://relishapp.com/rspec/rspec-core/v/2-6/dir/example-groups/shared-examples for more information

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文