Spec::Expectations lets you set expectations on your object by adding 2 methods to Object.
result.should == 42
result.should be_an_instance_of Book
Both methods take an optional Expression Matcher.
As well as defined Expression Matchers RSpec can create custom Matchers on the fly. A Ruby predicate is a method ending with a ? and returns true or false.
.should be_empty => .empty? #passes
.should_not be_empty => .empty? #fails
As well as be_ you can also use “be_a_” and “be_an_”. E.g.
3.should be_an_instance_of(Fixnum) => 3.instance_of?(Fixnum) #passes
Note that as of 2.11 RSpec includes a new variant to this syntax.
i.e. the old syntax is:
and the new:
There’s a rather good post about this here: