Skip to content

Instantly share code, notes, and snippets.

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 dbarrionuevo/f475b8a71a4773dd50f4b93c675eea87 to your computer and use it in GitHub Desktop.
Save dbarrionuevo/f475b8a71a4773dd50f4b93c675eea87 to your computer and use it in GitHub Desktop.
Rails Rspec model testing skeleton & cheat sheet using rspec-rails, shoulda-matchers, shoulda-callbacks, and factory_girl_rails.
# This is a skeleton for testing models including examples of validations, callbacks,
# scopes, instance & class methods, associations, and more.
# Pick and choose what you want, as all models don't NEED to be tested at this depth.
describe Model do
# Lazily loaded to ensure it's only used when it's needed
# Try to avoid @instance_variables if possible. They're slow.
let(:factory_instance) { build(:some_factory) }
describe 'ActiveRecord validations' do
# References
# http://guides.rubyonrails.org/active_record_validations.html
# https://github.com/thoughtbot/shoulda-matchers
# http://rubydoc.info/github/thoughtbot/shoulda-matchers/master/frames
# http://rubydoc.info/github/thoughtbot/shoulda-matchers/master/Shoulda/Matchers/ActiveModel
it { should validate_presence_of(:amount) }
it { should validate_inclusion_of(:type).in_array(Transaction::TYPES) }
it do
should validate_inclusion_of(:transaction_at)
.in_range(subject.offer.start_date.beginning_of_day..subject.offer.end_date.end_of_day)
.with_message('should be between the offer dates')
end
end
describe 'ActiveRecord associations' do
# References
# http://guides.rubyonrails.org/association_basics.html
# https://github.com/thoughtbot/shoulda-matchers
# http://rubydoc.info/github/thoughtbot/shoulda-matchers/master/frames
# http://rubydoc.info/github/thoughtbot/shoulda-matchers/master/Shoulda/Matchers/ActiveRecord
# Performance tip: stub out as many on create methods as you can when you're testing validations
# since the test suite will slow down due to having to run them all for each validation check.
#
# For example, assume a User has three methods that fire after one is created, stub them like this:
#
# before(:each) do
# User.any_instance.stub(:send_welcome_email)
# User.any_instance.stub(:track_new_user_signup)
# User.any_instance.stub(:method_that_takes_ten_seconds_to_complete)
# end
#
# If you performed 5-10 validation checks against a User, that would save a ton of time.
# Associations
it { should belong_to(:transaction) }
it { should belong_to(:created_by).class_name('User') }
it { should have_many(:refunds).dependent(:destroy) }
it { have_one(:transaction_detail) }
end
describe 'callbacks' do
# References
# http://guides.rubyonrails.org/active_record_callbacks.html
describe '#set_approval_status' do
it 'does something' do
end
end
describe '#properly_update_transaction_type' do
it 'does something' do
end
end
end
describe 'scopes' do
let(:pending) { create(:manual_transaction, approval_status: 'Pending') }
let(:approved) { create(:manual_transaction, approval_status: 'Approved') }
it '.pending returns pending transactions' do
end
it '.approved returns approved transactions' do
end
end
describe '.class_method_1' do
it 'does something' do
end
end
describe '.class_method_2' do
it 'does something' do
end
end
describe '#instance_method_1' do
it 'does something' do
end
end
describe '#instance_method_2' do
it 'does something' do
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment