Rspec Cheat Sheet
Rspec Cheat Sheet
Core API
Arbitrary Block
object.should_satisfy {|arg| ...} object.should_not_satisfy {|arg| ...} object.should_equal <value> object.should_not_equal <value> object.should == <value> object.should_be_close <val>, <delta> object.should_not_be_close <val>, <delta> object.should_be <value> object.should_not_be <value> object.should_predicate [args] object.should_be_predicate [args] object.should_not_predicate [args] object.should_not_be_predicate [args] object.should_be > <value> object.should_be < <value> object.should_match <regex> object.should_not_match <regex> object.should =~ <regex> object.should_be_an_instance_of <class> object.should_not_be_an_instance_of <class> object.should_be_a_kind_of <class> object.should_not_be_a_kind_of <class> object.should_respond_to <symbol> object.should_not_respond_to <symbol> proc.should_raise <exception> proc.should_not_raise <exception> proc.should_raise proc.should_not_raise proc.should_throw <symbol> proc.should_not_throw <symbol> proc.should_not_throw object.should_include <object> object.should_not_include <object object.should_have(<n>).things object.should_have_at_least(<n>).things object.should_have_at_most(<n>).things
Mock API
Creating a mock
mock = mock(<name>) mock = mock(<name>, <options>) mock = mock("blah", :null_object => true)
Equality
Expecting Messages
mock.should_receive(:<message>)
Identity
Expecting Arguments
Arbitrary Predicate
Pattern Matching
accepts anything that responds to all the message(s): #accepts a Fixnum for the second arg .with(a, duck_type(:abs, :div))
Receive Counts
.any_number_of_times .once .twice .exactly(n).times .at_least(:once) .at_least(:twice) .at_least(n).times
Ancestor Class
Type
Return Values
.and_return(v) .and_return(v1, v2, ..., vn)
implies consequtive returns & .at_least(n).times .and_return {...}
Raising
Throwing
Ordering
Containment
Collection Size
-f s -f specdoc --format s --format specdoc $spec -f s -s "An empty stack" examples/stack_spec.rb An empty - should - should - should stack accept an item when sent push complain when sent top complain when sent pop
1 context, 3 specifications, 0 failures -f r -f rdoc --format r --format rdoc spec -f r -s "An empty stack" examples/stack_spec.rb # An empty stack # * should accept an item when sent push # * should complain when sent top # * should complain when sent pop -f <custom formatter class> --format <custom formatter class> -c, --color, --colour
General Options
-v, --version -h, --help -d, --dry-run -r file, --require file
Output Formating
$spec -s "An empty stack" examples/stack_spec.rb ... Finished in 0.001903 seconds 1 context, 3 specifications, 0 failures