<p>OK, so instead of writing a whole bunch of access control specs, and duplicating them across many of my spec files, I'm looking to create a custom matcher. So instead of this:</p>

<pre><code>describe "access control" do
  it "should prevent access by non-logged-in users"
  it "should prevent access by normal users"
  it "should prevent access by editor users"
  it "should prevent access by admin users"
  it "should allow access by super admin users"
end
</code></pre>

<p>I want do something like this:</p>

<pre><code>lambda do
  get :index
end.should have_access_control(:allowed =&gt; [:super_admin], :disallowed =&gt; [:admin, :editor, :user])
</code></pre>

<p>Are there any examples or suggestions of how I can go about doing something like this?</p>

OK,
I have found a method of achieving this
, though it doesn't use a custom matcher. Include the following code in your spec_helper.rb:
```
```
def access_control (code, options={})
  options = {:allow => [], :disallow => []}.merge(options)

  options[:allow].each do |user|
    it "#{code} should allow #{user.to_s}" do
      login_as(user)
      eval code
      response.should_not redirect_to(login_path)
    end
  end

  options[:disallow].each do |user|
    it "#{code} should disallow #{user.to_s}" do
      login_as(user)
      eval code
      response.should redirect_to(login_path)
    end
  end
end
```
```
And call it as follows:
```
```
access_control("get :index", {:allow => [:super_admin], :disallow => [:quentin, :admin]})
```
```
You can then use it to build up a complete list of methods that should be restricted, and the users to which they are restricted to.