Abhishek Yadav
ரூபீ ப்ரோக்ராமர்
Co-organizer: Chennai.rb
Previous talk: Getting started with testing using rspec:
https://speakerdeck.com/h6165/lets-start-testing
Covers the premise and fundamentals.
=> Make testing simpler, easier and fun
Sometimes we have code
whose actual execution in the tests is costly/difficult
but not important to the outcomes of the test
# Method under test:
# (within User model)
def complete_registration
self.state = 'registered'
self.save
UserMailer.registered(self).deliver!
end
# In this test case, we are only checking the value of state
# We can have a separate test for email delivery,
# but in this one, we are not concerned with the email part
it 'should mark the state as registered' do
user.complete_registration
expect(user.state).to eq('registered')
end
Sometimes it is more complicated than a simple email:
Our code could be dependant on objects of other types (class/model).
While writing tests, we have to make sure these objects are created and configured correct to have the desired outcome
# Summary of comments, as they show on blog sidebars
# The summary contains the user's name,
# and the first twenty characters of the comment text
#
# Within Comment model,
# Comment has a column 'body' and
# Comment belongs_to :user
#
def comment_summary
user.name + "-" +
self.body[0..20]
end
# The method is dependent on the user object.
# While writing tests, we will need to create the user object too
# This may not seem like a big deal here,
# but when there are too many objects,
# or when object creation is complicated, it becomes important
Test double | Object that stands for the real object. Much like 'stunt double'. |
Stub | Object with a method overridden to return fake value |
Mock | Object with a method overridden to return fake value, and the method must be called |
Spy | Test double + testable expectation later |
Note:
Most popular: rspec-mocks, mocha
Ref: https://www.ruby-toolbox.com/categories/mocking
# Method under test: (extracted email sending to separate method)
def send_email
UserMailer.registered(self).deliver!
end
def complete_registration
self.state = 'registered'
self.save
send_email
end
# Stubbing the email step
it 'should mark the state as registered' do
allow(user).to receive(:send_email)
user.complete_registration
expect(user.state).to eq('registered')
end
# Method under test: (extracted email sending to separate method)
def send_email
UserMailer.registered(self).deliver!
end
def complete_registration
self.state = 'registered'
self.save
send_email
end
# We want to make sure 'send_email' is invoked,
# even if email is not actually sent
# This test fails if we remove the 'send_email' call from above code
it 'should try sending the email' do
expect(user).to receive(:send_email)
user.complete_registration
end
# Method under test: (extracted email sending to separate method)
def send_email
UserMailer.registered(self).deliver!
end
def complete_registration
self.state = 'registered'
self.save
send_email
end
# We want to make sure 'send_email' is invoked,
# even if email is not actually sent
# This test fails if we remove the 'send_email' call from above code
it 'should try sending the email' do
allow(user).to receive(:send_email)
user.complete_registration
expect(user).to have_received(:send_email)
end
# Within Comment model,
# Comment has a column 'body' and
# Comment belongs_to :user
def comment_summary
user.name + "-" +
self.body[0..20]
end
## Test
it "should be *Abhishek-This is the best*" do
# Instead of creating a real User object,
# which may be difficult, we create a double with just the name
user = double(name: 'Abhishek')
comment = Comment.new(body: 'This is the best')
expect(comment.comment_summary).to eq('Abhishek-This is the best')
end
class Partner < ActiveRecord::Base
## Create Location record, geocode it, and associate it
def update_locations(country, state, city)
if !country.blank?
l = Location.where(city: city, state: state, country: country).last
l = Location.new(city: city, state: state, country: country)
if l.latitude.nil? || l.longitude.nil?
lat, lng = Geocoder.coordinates("#{country}, #{state}, #{city}")
if lat && lng
l.latitude = lat
l.longitude = lng
end
end
l.save!
# Associate the Location with partner
pl = PartnerLocation.where(partner_id: self.id, location_id: l.id).last
if pl.nil?
pl = PartnerLocation.new(partner_id: self.id, location_id: l.id)
pl.save!
end
end
end
end
# Test case: 1
# Location does not exist, Geocoding is possible
it "test-1" do
partner = Partner.new
allow(partner).to receive(:id).and_return(1)
allow(Geocoder).to receive(:coordinates).and_return([13, 83])
partner.update_locations('India', 'Tamil Nadu', 'Chennai')
location = Location.last
expect([location.latitude, location.longitude]).to eq([13, 83])
end
Here we stub the Geocoder to return fake values we can use.
We also stub the partner#id to avoid saving it (it is used in the last part of the code)
# Test case: 2
# Location exists, Geocoding should not be invoked
it "test-2" do
partner = Partner.create
Location.create(country: 'India', state: 'Tamil Nadu', city: 'Chennai')
expect(Geocoder).to_not receive(:coordinates)
partner.update_locations('India', 'Tamil Nadu', 'Chennai')
end
Here we mock the Geocoder to make sure it is not called.
Thanks