Skip to content

Instantly share code, notes, and snippets.

@kriskhaira
Last active March 9, 2017 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kriskhaira/5c2b4d99aff6921d9e528fc7e3810ad0 to your computer and use it in GitHub Desktop.
Save kriskhaira/5c2b4d99aff6921d9e528fc7e3810ad0 to your computer and use it in GitHub Desktop.
testing scopes in RSpec
# PATTERN 1: describe > let + before + specify > expect
describe 'scopes' do
let(:offers) { create_list(:offer, 3, :available, :published, :shown) }
before do
offers.first.update(is_published: false)
offers.second.update(is_shown: false)
end
specify '.shown returns correct number of offers' do
expect(Offer.shown).not_to be_empty
end
specify '.usable returns correct number of offers' do
expect(Offer.usable).not_to be_empty
end
end
# PATTERN 2: describe > let + before + describe > subject + it > expect
describe 'scopes' do
let(:offers) { create_list(:offer, 3, :available, :published, :shown) }
before do
offers.first.update(is_published: false)
offers.second.update(is_shown: false)
end
describe '.shown' do
subject { Offer.shown }
it { expect(subject).not_to be_empty }
end
describe '.usable' do
subject { Offer.usable }
it { expect(subject).not_to be_empty }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment