public
Description: One-liner for creating custom matchers in rspec
Homepage:
Clone URL: git://github.com/xdotcommer/rspec-custom-matchers.git
xdotcommer (author)
Sat Mar 07 09:35:33 -0800 2009
commit  02f2a7af7247496eb1f425796f5c194d41e8786a
tree    4522b0550a98220e47d80a7cc274055de8199bae
parent  e8596f7130fe3bea8099c451455c44d962ed7664
name age message
file .gitignore Sat Mar 07 09:23:54 -0800 2009 removed .gem (accidentally commited) and added ... [remi]
file README.markdown Loading commit data...
file Rakefile Sat Mar 07 09:22:16 -0800 2009 added README, moved custom_matcher to lib/ to s... [remi]
file VERSION.yml Wed Jan 28 16:01:22 -0800 2009 Version bump to 0.1.1 [remi]
directory lib/ Wed Jan 28 15:49:15 -0800 2009 added a #description method to CustomMatcher so... [remi]
file rspec-custom-matchers.gemspec
directory spec/ Wed Jan 28 15:49:15 -0800 2009 added a #description method to CustomMatcher so... [remi]
README.markdown

RSpec Custom Matchers

This gem makes it really easy to define your own RSpec custom matchers in 1 line of code.

This class / project is created by xdotcommer and updated by remi to make it an easy-to-install RubyGem.

Install

sudo gem install rspec-custom-matchers -s http://gems.github.com

Usage

Wherever you want to include the matcher method, include CustomMatcher::Helper

Remember, if you want to be able to call matcher(:foo) in the body of a class, you might want to extend CustomMatcher::Helper instead of including it.

Personally, I like to keep all of my own custom matchers for a project in a module, so I do ...

require 'rspec-custom-matchers'

module MyMatchers
  extend CustomMatcher::Helper

  matcher(:be_divisible_by) { |number, divisor| number % divisor == 0 }
  matcher(:be_even)         { |even| even % 2 == 0 }
  matcher(:be_odd)          { |odd|  odd % 2 != 0  }
  matcher(:be_equal_to)
end

and then, in my spec_helper.rb

require 'my_matchers'

Spec::Runner.configure do |config|
  config.include MyMatchers # this will make the matchers available to your specs
end

That's how I do it!

TODO

  • should support matcher(:x){} as well as merb's Spec::Matcher.create, which has more features and can be found at merb-core/lib/merb-core/test/test_ext/rspec.rb