[rspec-users] custom matchers for request specs

Matt Wynne matt at mattwynne.net
Tue Feb 8 05:14:16 EST 2011

On 4 Feb 2011, at 09:30, Alan B wrote:

> Hi all, I'm trying to create a custom matcher that will visit a given
> path and check it's authenticated.
> Here's what I have so far (using rspec-rails and capybara):
> matcher :require_authentication do
>  match do |path|
>    visit path
>    #page.current_path.should == sign_in_path
>    page.current_path.should == '/sign_in'
>  end
> end
> The above version works, but when using the commented out line I get:
>  NameError:
>       undefined local variable or method `sign_in_path' for
> #<RSpec::Matchers::Matcher:0xbbd68ec>
> sign_in_path is one of the routes in my application and works fine
> inside a describe/it block.
> I don't understand why the matcher recognises visit() but not
> sign_in_path.

I'm surprised either of them do. This DSL is just a factory method for a matcher class. Unless that matcher class includes Capybara and the Rails routing methods module (can't remember the name off-hand) you won't have access to either of these methods in the matcher. Look at the non-DSL way to create a matcher and this will make more sense.

Also, you need to return true / false from #match, rather than using an assertion.


matt at mattwynne.net
07974 430184

More information about the rspec-users mailing list