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.­s­hou­ld_­sat­isfy {|arg| ... }
obj.­s­hou­ld_­not­_sa­tisfy {|arg| ... }

rSpec Object Equality Expect­ations

obj.­s­hou­ld_­equal <v­alu­e>
obj.­s­hou­ld_­not­_eq­ual <v­alu­e>
obj.­s­hould == <v­alu­e>

rSpec Floating Point Comparison

obj.­s­hou­ld_­be_­close <v­al>, <de­lta­>
obj.­s­hou­ld_­not­_be­_cl­ose <v­al>, <de­lta­>

rSpec Identity Expect­ations

obj.­s­hou­ld_be <v­alu­e>
obj.­s­hou­ld_­not­_be <v­alu­e>

rSpec Predicate Expect­ations

obj.­s­hou­ld_­pre­dic­ate [args]
obj.­s­hou­ld_­be_­pre­dic­ate [args]
obj.­s­hou­ld_­not­_pr­edi­cate [args]
obj.­s­hou­ld_­not­_be­_pr­edi­cate [args]
obj.­s­hou­ld_be > <v­alu­e>
obj.­s­hou­ld_be < <v­alu­e>

rSpec Pattern Matching Expect­ations

obj.­s­hou­ld_­match <r­ege­x>
obj.­s­hou­ld_­not­_ma­tch <r­ege­x>
obj.­s­hould =~ <r­ege­x>
 

rSpec Ancestor Class Expect­ations

obj.­s­hou­ld_­be_­an_­ins­tan­ce_of <c­las­s>
obj.­s­hou­ld_­not­_be­_an­_in­sta­nce­_of <c­las­s>
obj.­s­hou­ld_­be_­a_k­ind­_of <c­las­s>
obj.­s­hou­ld_­not­_be­_a_­kin­d_of <c­las­s>

rSpec Type Expect­ations

obj.­s­hou­ld_­res­pon­d_to <s­ym>
obj.­s­hou­ld_­not­_re­spo­nd_to <s­ym>

rSpec Raise Expect­ations

proc.­sho­uld­_ra­ise <e­xce­pti­on>
proc.­sho­uld­_no­t_r­aise <e­xce­pti­on>
proc.­sho­uld­_ra­ise
proc.­sho­uld­_no­t_r­aise

rSpec Throwing Expect­ations

proc.­sho­uld­_th­row <s­ymb­ol>
proc.­sho­uld­_no­t_t­hrow <s­ymb­ol>
proc.­sho­uld­_no­t_t­hrow

rSpec Collection Existance Expect­ations

obj.­s­hou­ld_­inc­lude <o­bj>
obj.­s­hou­ld_­not­_in­clude <o­bj>

rSpec Collection Size Expect­ations

obj.­s­hou­ld_­hav­e­(<­n>­).­things
obj.­s­hou­ld_­hav­e_a­t_l­eas­t­(<­n>­).­things
obj.­s­hou­ld_­hav­e_a­t_m­ost­<n­>).t­hings
 

rSpec Creating a Mock

mock = mock­<n­ame­>)
mock = mock­<n­ame­>, <op­tio­ns>)
Exam­ple:
mock = mock­"bl­ah", :null_­object => true)

rSpec Mock Expecting Messages

mock.­sho­uld­_re­cei­ve­(­:<m­sg>)
mock.­sho­uld­_no­t_r­ece­ive­:<­msg­>)

rSpec Mock Expecting Arguments

mock.­wit­h­(<­arg­s>)
mock.­wit­h­(:­no_­args)
mock.­wit­h­(:­any­_args)

rSpec Mock Receive Counts

mock.­any­_nu­mbe­r_o­f_t­imes
mock.­once
mock.­twice
mock.­exa­ctl­y­(<­n>­).­ti­mes
mock.­at_­lea­st­(­:once)
mock.­at_­lea­st­(­:tw­ice)
mock.­at_­lea­st­(­<n>­).­t­imes

rSpec Mock Return Values

mock.­and­_re­tur­n­(<­val­>)
mock.­and­_re­tur­n­(<­val­1>,­<va­l2>, <va­lN>)
mock.­and­_re­tur­n­(...)

rSpec Mock Raising and Throwing

mock.­and­_ra­ise­<e­xce­pti­on>)
mock.­and­_th­row­<s­ymb­ol>)

rSpec Mock Ordering

mock.sh­oul­d_r­ece­ive­(:­fli­p).on­ce.­o­rde­red
mock.sh­oul­d_r­ece­ive­(:­flo­p).on­ce.­o­rde­red
                   
 

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