Skip to content

Instantly share code, notes, and snippets.

@jilucev
Last active November 29, 2018 01:04
Show Gist options
  • Save jilucev/77c52aded9e698b53806b4e9d9ac83ea to your computer and use it in GitHub Desktop.
Save jilucev/77c52aded9e698b53806b4e9d9ac83ea to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require 'rails_helper'
require 'app/services/doc_gen/application_merge_tag_recipe'
describe DocGen::ApplicationMergeTagRecipe do
describe '#call' do
let(:account) { Fabricate.create(:account) }
let(:loan) { FactoryBot.create(:loan, account: account, team: account.team, liability: liability) }
let(:liability) { FactoryBot.create(:liability, :with_interest_rate, :with_payment_schedule) }
let(:result) { described_class.call(account, requestor) }
## this is a duplicated context just to give us more test runs
context 'some context' do
let(:requestor) { nil }
specify do
puts LenderSubmission::Models::LoanSubmission.test_me
LenderSubmission::Models::LoanSubmission.test_me = 'farmer kyle'
expect(result.keys).not_to include 'author_name'
end
end
before { loan }
context 'without requestor' do
let(:requestor) { nil }
specify do
puts LenderSubmission::Models::LoanSubmission.test_me
LenderSubmission::Models::LoanSubmission.test_me = 'farmer jill'
expect(result.keys).not_to include 'author_name'
end
end
context 'with requestor' do
let(:requestor) { Fabricate.create(:api_key, team: account.team) }
specify do
puts LenderSubmission::Models::LoanSubmission.test_me
expect(result.keys).to include 'author_name'
end
end
end
end
## when you run this test with bundle exec rspec spec/services/doc_gen/application_merge_tag_recipe_spec.rb --order defined, which ensures the tests are run sequentially, you can see that the state of the globals defined in rails_helper carries over to the next test.
# frozen_string_literal: true
ENV['RAILS_ENV'] ||= 'test'
ENV['FULL_CONTACT_API_KEY'] = '61202499dbd3b90b'
ENV['WORKERS_PERFORM_JOBS_SYNCHRONOUSLY'] = 'false'
require File.expand_path('../../config/environment', __FILE__)
require 'action_controller/railtie'
require 'active_record/railtie'
require 'active_support'
require 'config/initializers/global_constants'
require 'devise'
require 'fabrication'
require 'factory_bot_rails'
require 'faker'
require 'fullcontact'
require 'mock_redis'
require 'ostruct'
require 'paper_trail/frameworks/rspec'
require 'pry'
require 'pundit/rspec'
require 'rspec/rails'
require 'shoulda/matchers'
require 'sidekiq/testing'
require 'simplecov'
require 'spec_helper'
require 'webmock/rspec'
abort("The Rails environment is running in production mode!") if Rails.env.production?
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
Time.zone = 'UTC'
begin
ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
puts e.to_s.strip
exit 1
end
module Appsignal
def self.method_missing(*)
nil && super
end
def self.respond_to_missing?(*)
true || super
end
end
ActionDispatch::Routing::RouteSet.send(:define_method, :default_url_options) do
{ host: 'localhost:3001' }
end
SimpleCov.start
FullContact.configure do |config|
config.api_key = '61202499dbd3b90b'
end
class ActiveRecord::Base
def self.===(other)
return self == false if other == REDIS
super(other)
end
end
class LenderSubmission::Models::LoanSubmission
## we are setting a value that lives on this class so we can inspect it with each test run and tell whether it is re-initialized or holds the last value it was set to.
@@test_me = 'farmer billiam'
def self.test_me
@@test_me
end
def self.test_me=(str)
@@test_me = str
end
def self.find_by!(*)
OpenStruct.new(lender_code: 'Foo')
end
end
class Lender
def self.find_by!(*)
OpenStruct.new(registration_name: 'Bar')
end
end
RSpec.configure do |config|
config.include ActiveRecordHelpers
config.include AuthorizationHelpers
config.include ComparableAddress
config.include Devise::TestHelpers, type: :controller
config.include DeviseControllerHelpers, type: :devise
config.include ExpectHelpers
config.include FactoryBot::Syntax::Methods
config.include Requests::Authentication, type: :request
config.include Requests::Authentication::Controller, type: :controller
config.include Requests::ElasticsearchStubbing, type: :request
config.include Requests::ElasticsearchStubbing, type: :search
config.include Requests::HttpCalls, type: :request
config.include Requests::ResponseHelpers, type: :controller
config.include Requests::ResponseHelpers, type: :request
config.include Shoulda::Matchers::ActiveModel, type: :model
config.include Shoulda::Matchers::ActiveRecord, type: :model
config.include_context 'common database methods & data'
config.include_context 'regexes'
config.include_context 'team scope', type: :controller
config.include_context 'team scope', type: :operation
config.include_context 'team scope', type: :request
config.infer_base_class_for_anonymous_controllers = false
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
config.define_derived_metadata do |meta|
meta[:aggregate_failures] = true
end
config.expect_with(:rspec) do |config|
config.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with(:rspec) do |config|
config.verify_doubled_constant_names = true
end
config.around(:each, :vcr) do |example|
name = example.metadata[:full_description].split(/\s+/, 2).join('/').underscore.gsub(/[^\w\/]+/, '_')
VCR.use_cassette(name) { example.call }
end
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each, type: :request) do
# Disable thread run so that it doesn't interfere with other specs
allow(Thread).to receive(:new)
end
config.before(:each) do
DatabaseCleaner.start
Sidekiq::Testing.fake!
allow_any_instance_of(ActiveRecord::Base).to receive :touch
stub_const('REDIS', MockRedis.new) unless Object.const_defined?('REDIS') && REDIS.is_a?(MockRedis)
allow(Sidekiq).to receive(:redis) do |&block|
fail ArgumentError, 'requires a block' unless block
block.(REDIS)
end
method_key_paths = {
call: {
Publish: {
create: proc {},
update: proc {},
bulk_create: proc {},
message: proc {}
},
Auth: {
sign_token: proc {}
}
}
}
stubbed_rpc_service = MockProxy.new(method_key_paths)
allow(BERTRPC::Service).to receive(:new).and_return stubbed_rpc_service
end
config.after(:each) do
DatabaseCleaner.clean
Sidekiq::Worker.clear_all
Thread.current.keys.each { |key| Thread.current[key] = nil }
end
config.after(:each, type: :request) do
Sidekiq::Worker.clear_all
end
end
RSpec::Matchers.define_negated_matcher :not_change, :change
RSpec::Matchers.define_negated_matcher :not_significantly_change, :significantly_change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment