Skip to content

Instantly share code, notes, and snippets.

@bchewy
Last active July 31, 2019 07:09
Show Gist options
  • Save bchewy/ceaf3bdf9192ec902d8f5d80006e4080 to your computer and use it in GitHub Desktop.
Save bchewy/ceaf3bdf9192ec902d8f5d80006e4080 to your computer and use it in GitHub Desktop.
RSpecs cheatsheet

RSpec Cheatshet

This gist will give you a quick insight on how you can write unit tests with rspec!

Mini-topics covered:

  • Defining test cases
  • Writing your own factory
  • Mocking methods/classes
  • Expected test results;
  • Writing controller specs
  • Before you do....

Defining your test cases;

You would define your test caess within an it statement; EG:

context 'when logged in' do
  it { is_expected.to respond_with 200 }
end

What's it in this case?; It's the subject of the rspec; an example is shown here

subject(:hero) { Hero.first }
it "carries a sword" do
  expect(hero.equipment).to include "sword"
end

Another way to write it would be like such - using let or let! statements!

describe '#hero carries a sword' do
  let(:hero) { Hero.first }
  it 'carries a sword' do
    expect(hero.equipment).to include "sword"
  end
end

Creating a factory with factorygirl or factorybot

What is factory bot?

let(:object) { create(:factory_name, variable: 'deseriedvalueofvariable') }

In the above command, you create an object from the factory defined in /spec/factories/factory_name.rb, overriding it's default value of variable in the factory with the 'deseriedvalueofvariable'

Stubbing objects and methods; (Really important!)

allow(Class).to receive(:method_name).and_return(stubbed_value) In the above command; you specifically stub or mock the return value of Class.method_name returning stubbed_value!

allow(Class)
  .to receive(:method_name)
  .with(argumentOne, argumentTwo)
  .and_return(expectedReturn)

In the scenario you want to make sure the arguments are allowed and skimpped through quickly too; Add the .with() method. The above code shows you how easy it is to write one.

Expected tests results

With rspec's unit tests; you can make use of the expect method to expect anything as a return. For instance:

Expects the result to be successful; ie: 200 status code
it 'expects the result to be successful'
  expect(result).to be_successful
end
Expects the result to be equal to your variable; Json in our case
it 'expects result body to be the same as desired result'
  expect(result.body).to eq(desired_result.as_json)
 end
Expects to be a particular kind of class; Integer in our case
it 'expects result body ['id] variable to be of Integer type'
  expect(result.body['id']).to be_a_kind_of(Integer)
end

Writing controller specs

Writing specs for controllers can be tough, but let's make it easy by summarizing up these snippets.

Make sure you have the type as controller
RSpec.describe Api::V1::SampleController, type: :controller do
end
Different kinds of methods; the index method, show, create, destroy, update!

Do note that these parameters are defined as:

  let(:params) do
    {
      id: object.id
    }
  end

and made use of like such:

get :index
get :show, params: {id: object.id}
post :create, params: params
put :update, params: params
delete :destroy, params: params

Above you'll see the the positive test scenarios - what the controller should respond with; but below... you'll find some bad scenarios!

get :show, params: {id: fakeobject.id}
post :create, params: error_params
put :update, params: error_params
delete :destroy, params: error_params

You can also make use of the formatting by specifying a format for the mock request to follow like such

get :show, params: {id: object.id}, format: :jsonapi

Before you do...

This section runs through what is the before statement in rspec;

before do
  # Do something here
  hero.equipment << "sword"
end
Helpful references;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment