[rspec-users] surprising...

Ashley Moran ashley.moran at patchspace.co.uk
Mon Nov 30 06:45:35 EST 2009


On Nov 29, 2009, at 6:33 am, rogerdpack wrote:

> It is somewhat surprising to me, as a newbie, to have to assert
> a.should be_a(Hash)

Hi Roger

Once you see the matcher (ie be_a) as something that returns a matcher object, it makes a lot more sense.  My brain is now wired to give much more weight to the thing on the right.

You might find it instructive/enlightening/life-changing (possibly) to have a go at writing some custom matchers.  From the RSpec docs[1]:

  bob.current_zone.should eql(Zone.new("4"))

becomes ->

  bob.should be_in_zone("4")

which is implemented with ->

  Spec::Matchers.define :be_in_zone do |zone|
    match do |player|
      player.in_zone?(zone)
    end
  end

Although I've just realised that RSpec's dynamic be_* matcher actually gives you that for free...

HTH

Ashley


[1] http://rspec.rubyforge.org/rspec/1.2.9/classes/Spec/Matchers.html


-- 
http://www.patchspace.co.uk/
http://www.linkedin.com/in/ashleymoran



More information about the rspec-users mailing list