RSpec matcher helper

| |

I've finally written more than one RSpec matcher. Creating a matcher essentially involves defining a class and a method that creates an instance of that class. Rather redundant, resulting in my RSpec matcher helper:

def defmatcher(name, &code)
  klass = Class.new
  klass.module_eval(&code)
  
  Object.send :define_method, name.to_s do |*args|
    klass.new(*args)
  end
end

That little method, defmatcher, creates the class and method for you. It saves some typing. An example can be found in the full source file, available here.

Update (2008/06/28): RSpec was having trouble keeping this all in one file. I created a project at GitHub to host this for the time being: http://github.com/sneakin/spec_matcher_helper/

AttachmentSize
spec_matcher_dsl.rb1.45 KB

Ad's by Google