Last Updated: February 25, 2016
·
1.889K
· slawosz

Given, When, Then in Rspec

There is a wide known convention for test unit: Given, When, Than.
Here is how to write nice example in Rspec:

Instead:

it 'should perform successfull request' do
  # given
  resource = Factory(:resource)

  #when
  get :show, id: resource.id

  #than
  response.should be_successfull
end

Use rspec magic:

context 'successfull response' do
  let(:resource) { Factory(:resource)

  before { get :show, id: resource.id }

  specify { response.should be_successfull }
end