Rspec Cheat Sheet



In my previous posts I did some writngs on UTPLSQL and ruby-plsql.

  1. Rspec Matchers Cheat Sheet
  2. Rspec Cheat Sheet
  3. Rspec Expectations Cheat Sheet
  4. Rspec Capybara Cheat Sheet

Rspec-Rails Cheat Sheet. Table of contents. Time helpers; Matchers. Sass is a preprocessor scripting language that is interpreted or compiled into Cascading Style. If you run the above in a directory of pictures, it will resize all pictures in the current directory. For my needs, I passed -Z to keep the ratio, and a maximum height of 1024 pixels. As always, make sure to keep the original in case you don’t like the results! Briefly look over RSpec’s other matchers, if you haven’t done so already. Briefly look over the RSpec styling and syntax recommended by BetterSpecs and read through the first six guidelines. The RSpec Cheat Sheet should help you avoid Googling every new bit of syntax.

For long time, while developing Oracle code I was using ruby-plsql to do test driven development for SQL and PL/SQL.
I used to frequently forget how to use some of the functionalities of ruby-plsql, specially after having a longer break and so each time I was referring the Unit Tests supplied for the ruby-plsql library as a reference. They are really nicely documenting how things work and how can they be used. It usually took me few minutes to find the thing I needed.

While I was actively using ruby-plsql for testing Oracle SQL and PLSQL code, it never occurred to me, that it would be nice to have some kind of cheat sheet, and so I wasted the valuable seconds and minutes to figure out how to do things I needed to do.

This week however, Turntablez posted a suggestion on ruby-plsql-spec github project page that a cheat sheet would be something of a use.

It was a really great idea, saves precious minutes when you need to use something you’re not used to or just don’t do every day.

So here it is, fully downloadable, editable and hopefully usable Cheat Sheet for ruby-plsql.

It’s not a reference, it’s not complete (or official), but hopefully it’s a good overview and a fast help if you need to see how things work.

Update
If you prefer a more visual/printable form, you might want to check out the same Cheat Sheet on http://www.cheatography.com/jgebal/cheat-sheets/ruby-plsql-cheat-sheet/

Or just download the PDF version.
File manager for iphone mac.

If you like it, share it, rate it, comment it.

Hooks

Use instance variables to make objects available to examples

customer is a local variable and will not be available@customer is an instance variable and will be available

describe 'Car' do describe 'attributes' do

endend

Let method

let(:car) {..} #lazy executed, waitslet!(:car) {..} #eager executed, runs right awayLet method should be used for variables

let(:car) { Car.new }it 'allows reading for :wheels' do expect(car.wheels).to eq(4) #car.wheels is a METHOD callend

Setting a subject

Shorthand thats available, don't have to use it.Can use 'subject' instead of 'let' if variable is subject of example

let(:subject) { Car.new } #var is subject of examplesubject { Car.new } #shorthand, same as above, because so common. subject!{..} also eager executed

it 'allows reading for :wheels' do expect(subject.wheels).to eq(4) #car.wheels is a METHOD callend

Implicitly defined subjects

Describe can accept either a string or a class name

describe Car it '..'

#instead of

describe 'car' subject { Car.new }

Sheet

Shared Examples

It's a shared file that can be used across different classes

Sheet

Test Doubles. What are test doubles?

Cheat

Test doubles are an object that stands in for another objectAliases: Doubles, mocks, stubs, fakes, spies, dummies

Double/Mock

A simple object preprogrammed with expectations and responses as preparation for the calls it will receive

Stub - a fake method

An instruction to an object to return a specific response to a method call

Double Example

it 'allows stubbing methods' do dbl = double('Chant') #create object allow(dbl).to receive(:hey!) #give object thing to do, or a 'fake method' here expect(dbl).to respond_to(:hey!)end

it 'allows stubbing methods' do dbl = double('Chant') #create object allow(dbl).to receive(:hey!).and_return('Ho!')#give object thing to do, or a 'fake method' here, and return {'Ho!'} allow(dbl).to receive(:hey!) {'Ho!'} #same as above, but can use logic in the return block here expect(dbl.hey!).to eq('Ho!')end

it 'allows setting multiple responses' do die = double('Die') allow(die).to receive(:roll).and_return(1,5,2,6) expect(die.roll).to eq(1) expect(die.roll).to eq(5) expect(die.roll).to eq(2) expect(die.roll).to eq(6)end

Partial Test Doubles

Adding a method .to receive(:some_non_existant_method).and_return('Something it wouldn't normally return')

Message Expectations

Rspec Matchers Cheat Sheet

Ruby sends 'messages' to objects to call methods. RSpec can set expectations about those messages

Cheat

#instead of allow(dbl).to receive(:hey!).and_return('Ho!')expect(dbl.hey!).to eq('Ho!')

#it looks likeexpect(dbl).to receive(:hey!).and_return('Ho!') #this is now an expect statementdbl.hey! #call method by itself

Message Argument Constraints

This is using .withexpect(dbl).to receive(:sort).with(any_args) #any_args is the defaultexpect(dbl).to receive(:sort).with(name)dble.sort('name')

Message Count Constraints

Rspec Cheat Sheet

Rspec Cheat Sheet

Verifies how many times a method gets called during an example

oncetwiceexactly(n)at_least(:once) at_least(:twice) at_least(n).timesat_most(:once) at_most(:twice) at_most(n).times

expect(post).to receive(:like).exactly(3).timespost.like(:user => 'Bob1')post.like(:user => 'Bob2')post.like(:user => 'Bob3')

Rspec Expectations Cheat Sheet

Spies

New type of test double, called spies

Rspec Capybara Cheat Sheet

In a non spy test double, set expectation before we call the method

Non Spy Test Double. Set expectation before call the mock object.it 'expects a call before it is received' do dbl = double('Chant') expect(dbl).to receive(:hey!).and_return('Ho!') dbl.hey!end

Spy Test Double. Set expectation after the requestit 'expects a call after it is received' do dbl = spy('Chant') #spy allow(dbl).to receive(:hey!).and_return('Ho!') #spy will work with this line crossed out dbl.hey! expect(dbl).to have_received(:hey!) #have_receivedend