predicate_matchers 3
Updated on 5/2/2007
In RSpec-0.8 if you say …
File.should_exist(path)
... the expectation passes if File.exist?(path). Here’s how that should look in RSpec-0.9, with the underscore removed:
File.should exist(path)
Supporting this for any arbitrary predicate would require more method_missing magic than we were willing to stomach, so we added a means of easily declaring methods like this yourself. We’ve supplied #exist out of the box, but you can add your own with a simple declaration.
Here’s how you do this for an individual behaviour:
describe Fish do
predicate_matchers[:swim] = :can_swim?
it "should swim" do
Fish.new.should swim
end
end
And here’s how you define them globally, so they are available in every example in your suite:
Spec::Runner.configure do |config|
config.predicate_matchers[:swim] = :can_swim?
end
Trackbacks
Use the following link to trackback from your own site:
http://blog.davidchelimsky.net/articles/trackback/233

Is that backward or do I misunderstand? predicate_matchers[:can_swim?] = :swim and not: predicate_matchers[:swim] = [:can_swim?]
Is the hash keyed by the word used in the test (“swim”) so that you have to do a “backward” search? Or is it the other way round? Maybe it’s my ruby-ignorance showing, but it sure looks backward.
No – this is correct – at least it’s how it works. Aslak and I debated which should be the key and which the value and felt this way made more sense. Perhaps we were wrong!
Any other opinions?
I went ahead and changed this. A couple of others had the same reaction you did. It’s in the trunk now and will be released as part of rspec 0.9.2 within the next day or so.
Cheers