Skip to content

Instantly share code, notes, and snippets.

@gabeodess
Last active August 29, 2015 14:21
Show Gist options
  • Save gabeodess/3d148172e771214842a6 to your computer and use it in GitHub Desktop.
Save gabeodess/3d148172e771214842a6 to your computer and use it in GitHub Desktop.
Overriding classes for test cases.
require 'test_helper'
class CreateShippoTransactionJobTest < ActiveJob::TestCase
test "the truth" do
@order = FactoryGirl.create(:order)
assert_enqueued_with(:job => CreateShippoTransactionJob) do
@shipment = @order.create_shipment!
end
assert_equal 'processing', @shipment.status
assert_difference "Shippo::Transaction::STORE.length" do
assert_change "@shipment.reload.status", "processed" do
# => Job should not create new transaction if one already exists
2.times do
CreateShippoTransactionJob.perform_now(@shipment)
end
end
end
end
end
# app/models.transaction.rb
class Shippo::Transaction < Shippo::Base
@@endpoint = File.join(API_ENDPOINT, "transactions")
end
# test/stubs/shippo/transaction.rb
Shippo::Transaction.class_eval do
STORE = {}
def self.create(options = {})
options[:id] = rand
STORE[options[:id]] = options
end
end
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'mocha/mini_test'
require 'sidekiq/testing'
require 'assertions'
require 'capybara/rails'
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
pp("Running with DEBUG=#{ENV['DEBUG']}") if ENV['DEBUG']
# Load Stripe stubbing files
Dir.glob(File.expand_path(Rails.root.join('test/stubs/stripe/*.rb').to_s, __FILE__)).each do |file|
require file
end
# Load Shippo stubbing files
Dir.glob(File.expand_path(Rails.root.join('test/stubs/shippo/*.rb').to_s, __FILE__)).each do |file|
require file
end
require Rails.root.join('test/webhook_factory.rb').to_s
class ActionDispatch::IntegrationTest
# Make the Capybara DSL available in all integration tests
include Capybara::DSL
# def teardown
# Capybara.use_default_driver
# end
end
class ActionController::TestCase
include Devise::TestHelpers
end
class ActiveSupport::TestCase
include ActiveJob::TestHelper
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
# =======================================================
# = Shared connection configuration to support JS tests =
# =======================================================
class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil
def self.connection
@@shared_connection || ConnectionPool::Wrapper.new(:size => 1) { retrieve_connection }
end
end
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
# class ActiveRecord::Base
# mattr_accessor :shared_connection
# @@shared_connection = nil
#
# def self.connection
# @@shared_connection || retrieve_connection
# end
# end
#
# # Forces all threads to share the same connection. This works on
# # Capybara because it starts the web server in a thread.
# ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
# ======================
# = Active Job Helpers =
# ======================
def timely_jobs
enqueued_jobs.select{ |i| i.performed.blank? and (i.at.blank? or (i.at <= Time.now.to_i)) }
end
def run_jobs(options = {})
enqueued_jobs.each{ |job| job.reject!{ |k,v| k.to_s == 'at' } }
run_timely_jobs(options)
end
# TODO: upgrading to rails 4.2.1 breaks activejob tests. Something about serialization
def run_timely_jobs(options = {})
while (jobs = timely_jobs).present?
jobs.each do |job|
begin
job.job.perform_now(*preprocess_job_arguments(job.args))
job['performed'] = true
rescue Exception => e
job['attempts'] = (job['attempts'] || 0) + 1
job['last_error'] = e
job[:at] = 5.minutes.from_now.to_i
raise if !options.silence_errors
end
end
self.performed_jobs += enqueued_jobs.select(&:performed)
enqueued_jobs.reject!(&:performed)
end
end
def preprocess_job_arguments(args)
args.map{ |i| preprocess_job_argument(i) }
end
def preprocess_job_argument(arg)
if arg.is_a?(Hash)
if arg.keys.first == "_aj_globalid"
path = arg["_aj_globalid"].split("/")
return path[-2].constantize.find(path[-1])
end
return arg.reject{ |k,v| k == "_aj_symbol_keys" }
else
return arg
end
end
class ActionController::TestCase
include Devise::TestHelpers
end
$ spring rake test test/jobs/create_shippo_transaction_job_test.rb
Run options: --seed 48636
# Running:
/Users/gabeodess/Sites/share_coffee/test/jobs/create_shippo_transaction_job_test.rb:15: warning: toplevel constant STORE referenced by Shippo::Transaction::STORE
/Users/gabeodess/Sites/share_coffee/test/jobs/create_shippo_transaction_job_test.rb:15: warning: toplevel constant STORE referenced by Shippo::Transaction::STORE
@gabeodess
Copy link
Author

When I run my test suite, I would like my Transaction class to have a constant "STORE" that will keep all Transaction records created during that test.

The warnings in text_output.txt suggest I'm going about this the wrong way, but I'm not sure what I'm doing wrong or what to do differently.

Can someone help me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment