Skip to content

Instantly share code, notes, and snippets.

@dhempy
Created February 5, 2015 19:28
Show Gist options
  • Save dhempy/342a841b5650e842bbe7 to your computer and use it in GitHub Desktop.
Save dhempy/342a841b5650e842bbe7 to your computer and use it in GitHub Desktop.
Example of lazy execution in rspec - let vs. let! vs. object mention
require 'rails_helper'
RSpec.describe V1::AppsController, type: :controller do
let(:user) { create(:user) }
describe '[mention] DELETE destroy when you are not logged in' do
let(:app) { create(:app, account: user.account) }
let(:make_request) { delete :destroy, id: app, format: :json }
it 'does not delete the app (.count)' do
app
expect { make_request }.not_to change(App, :count)
end
end
describe '[let] DELETE destroy when you are not logged in' do
let(:app) { create(:app, account: user.account) }
let(:make_request) { delete :destroy, id: app, format: :json }
it 'does not delete the app (.count)' do
expect { make_request }.not_to change(App, :count)
end
end
describe '[let!] DELETE destroy when you are not logged in' do
let!(:app) { create(:app, account: user.account) }
let(:make_request) { delete :destroy, id: app, format: :json }
it 'does not delete the app (.count)' do
expect { make_request }.not_to change(App, :count)
end
end
end
V1::AppsController
[mention] DELETE destroy when you are not logged in
does not delete the app (.count)
[let] DELETE destroy when you are not logged in
**** does not delete the app (.count) (FAILED - 1) ****
[let!] DELETE destroy when you are not logged in
does not delete the app (.count)
Failures:
1) V1::AppsController [let] DELETE destroy when you are not logged in does not delete the app (.count)
Failure/Error: expect { make_request }.not_to change(App, :count)
expected #count not to have changed, but did change from 0 to 1
# ./spec/controllers/v1/delete_test_3.rb:21:in `block (3 levels) in <top (required)>'
Finished in 0.85543 seconds (files took 3.77 seconds to load)
3 examples, 1 failure
Failed examples:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment