Show Menu
Cheatography

rSpec Core API and Mock API Cheat Sheet by

Quick reference to many expectation methods for testing ruby code via rSpec and optionally Mock.

rSpec Object Block Expect­ations

obj.should­_sa­tisfy {|arg| ... }
obj.should­_no­t_s­atisfy {|arg| ... }

rSpec Object Equality Expect­ations

obj.should­_equal <va­lue>
obj.should­_no­t_equal <va­lue>
obj.should == <va­lue>

rSpec Floating Point Comparison

obj.should­_be­_close <va­l>, <de­lta>
obj.should­_no­t_b­e_close <va­l>, <de­lta>

rSpec Identity Expect­ations

obj.should_be <va­lue>
obj.should­_not_be <va­lue>

rSpec Predicate Expect­ations

obj.should­_pr­edicate [args]
obj.should­_be­_pr­edicate [args]
obj.should­_no­t_p­red­icate [args]
obj.should­_no­t_b­e_p­red­icate [args]
obj.should_be > <va­lue>
obj.should_be < <va­lue>

rSpec Pattern Matching Expect­ations

obj.should­_match <re­gex>
obj.should­_no­t_match <re­gex>
obj.should =~ <re­gex>
 

rSpec Ancestor Class Expect­ations

obj.should­_be­_an­_in­sta­nce_of <cl­ass>
obj.should­_no­t_b­e_a­n_i­nst­ance_of <cl­ass>
obj.should­_be­_a_­kind_of <cl­ass>
obj.should­_no­t_b­e_a­_ki­nd_of <cl­ass>

rSpec Type Expect­ations

obj.should­_re­spo­nd_to <sy­m>
obj.should­_no­t_r­esp­ond_to <sy­m>

rSpec Raise Expect­ations

proc.should­_raise <ex­cep­tio­n>
proc.should­_no­t_raise <ex­cep­tio­n>
proc.should­_raise
proc.should­_no­t_raise

rSpec Throwing Expect­ations

proc.should­_throw <sy­mbo­l>
proc.should­_no­t_throw <sy­mbo­l>
proc.should­_no­t_throw

rSpec Collection Existance Expect­ations

obj.should­_in­clude <ob­j>
obj.should­_no­t_i­nclude <ob­j>

rSpec Collection Size Expect­ations

obj.should­_have(<n>).things
obj.should­_ha­ve_­at_­least(<n>).things
obj.should­_ha­ve_­at_most(<n>).things
 

rSpec Creating a Mock

mock = mock(<na­me>)
mock = mock(<na­me>, <op­tio­ns>)
Example:
mock = mock("­bla­h", :null_­object => true)

rSpec Mock Expecting Messages

mock.should­_re­ceive(:<m­sg>)
mock.should­_no­t_r­eceive(:<m­sg>)

rSpec Mock Expecting Arguments

mock.with(<ar­gs>)
mock.with(:no_args)
mock.with(:any_args)

rSpec Mock Receive Counts

mock.any_nu­mbe­r_o­f_times
mock.once
mock.twice
mock.exactly(<n>).times
mock.at_least(:once)
mock.at_least(:twice)
mock.at_least(<n>).times

rSpec Mock Return Values

mock.and_return(<va­l>)
mock.and_return(<va­l1>­,<v­al2­>, <va­lN>)
mock.and_return(...)

rSpec Mock Raising and Throwing

mock.and_raise(<ex­cep­tio­n>)
mock.and_throw(<sy­mbo­l>)

rSpec Mock Ordering

mock.shoul­d_r­eceive(:flip).once.ordered
mock.shoul­d_r­eceive(:flop).once.ordered
                   
 

Comments

Wow, this is great! I really like how you've set out the info. Nice job!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Cypressio Cheat Sheet
          Selenium WebDriver Cheat Sheet Cheat Sheet
          ISTQB Test Automation Engineering Cheat Sheet

          More Cheat Sheets by CITguy

          jasmine JS testing Cheat Sheet
          Extending Ruby with C - Part 1 Cheat Sheet
          *nix users and groups Cheat Sheet