Skip to content

Instantly share code, notes, and snippets.

@jmzbond
Last active August 29, 2015 14:08
Show Gist options
  • Save jmzbond/17342b91fa437a6c339e to your computer and use it in GitHub Desktop.
Save jmzbond/17342b91fa437a6c339e to your computer and use it in GitHub Desktop.
Sucker Punch testing
# REVISED TEST FILES
describe TwilioController, job: true do
before { @rentalrequest = FactoryGirl.create(:rental_request) }
it "enqueues a RoadRunner Twilio Alert job" do
get :alert, rentalrequest_id: @rentalrequest.id
RoadrunnerTwilioAlert.jobs.count.should == 1 # not sure what the right syntax is here?
end
end
describe RoadrunnerTwilioAlert, job: true do # not sure what type of test this qualifies as under Rspec; at least, putting this as a controller test didn't seem to work
before do
@rentalrequest = FactoryGirl.create(:rental_request)
RoadrunnerTwilioAlert.new.async.perform(@rentalrequest.id) # will this call in the job appropriately?
end
it "should have updated the record"
@rentalrequest.twilio_alert.should == "text"
end
end
######################################################
# spec/controllers/twilio_controller_spec.rb
describe TwilioController, job: true do
before { @callrentalrequest = FactoryGirl.create(:call_rental_request) }
before { @emailrentalrequest = FactoryGirl.create(:email_rental_request) }
it "call test" do
get :alert, rentalrequest_id: @callrentalrequest.id
assigns(:rentalrequest).twilio_alert.should == "called"
end
it "email test" do
get :alert, rentalrequest_id: @emailrentalrequest.id
ActionMailer::Base.deliveries.size == 1
end
end
# controllers/twilio_controller.rb
class TwilioController < ApplicationController
def alert
RoadrunnerTwilioAlert.new.async.perform(params[:rentalrequest_id])
# After the job is complete, ensures the test can find the @rentalrequest variableclas
@rentalrequest = RentalRequest.find(params[:rentalrequest_id])
...
end
end
# jobs/roadrunner_twilio_alert.rb
Class RoadrunnerTwilioAlert
include SuckerPunch::Job
def perform(rentalrequest_id)
ActiveRecord::Base.connection_pool.with_connection do
@rentalrequest = RentalRequest.find(rentalrequest_id)
...#perform twilio
if twilio.call?
@rentalrequest.update_attributes(twilio_alert:"called")
else
ErrorMailer.twilio_email(params).deliver
end
end
end
end
######################
# Outcome of test is that ONLY the email tests pass. All the tests where I'm expecting hte record to be updated with a "called" flag return:
#Failure/Error: assigns(:rentalrequest).twilio_alert.should == "call"
#expected: "call"
#got: nil (using ==)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment