Skip to content

Instantly share code, notes, and snippets.

@andychongyz
Created September 9, 2016 03:28
Show Gist options
  • Save andychongyz/ec298723755fdf6904c785914aad2d41 to your computer and use it in GitHub Desktop.
Save andychongyz/ec298723755fdf6904c785914aad2d41 to your computer and use it in GitHub Desktop.
Spec Helper
require "codeclimate-test-reporter"
require 'webmock/rspec'
require 'sidekiq/testing'
require 'capybara/rspec'
require 'capybara-screenshot/rspec'
require 'capybara/poltergeist'
Sidekiq::Testing.fake!
CodeClimate::TestReporter.start
WebMock.disable_net_connect!(allow: "codeclimate.com")
# DEFAULT_PORT = 9827
Capybara.javascript_driver = :poltergeist
# Capybara.server_boot_timeout = 9000
# Capybara.asset_host = 'http://localhost:3000'
# Capybara.server_port = DEFAULT_PORT
#Capybara.app_host = "http://lvh.me:#{Capybara.server_port}"
# Capybara::Screenshot.prune_strategy = :keep_last_run
# Capybara::Screenshot.autosave_on_failure = true
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause this
# file to always be loaded, without a need to explicitly require it in any files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, make a
# separate helper file that requires this one and then use it only in the specs
# that actually need it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
config.expect_with :rspec do |c|
c.syntax = :expect # Disable old `should` syntax
end
config.before(:each) do
Sidekiq::Worker.clear_all
end
config.before(:each) do
stub_request(:get, /http:\/\/127.0.0.1:\d*\/__identify__/).
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => "", :headers => {})
stub_request(:get, "http://pro.ip-api.com/json/?key=#{ENV['IP_API_KEY']}&output=json").
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => "{ \"status\": \"success\", \"lat\": 0, \"lon\": 0, \"query\": \"0.0.0.0\", \"isp\": \"TM\" }", :headers => {})
stub_request(:post, "https://onesignal.com/api/v1/players?output=json").
with(headers: {'Content-Type' => 'application/json'}).
to_return(status: 200, body: "{\"success\": true,\"id\":\"c43d1b22-75ae-4ab6-9193-4b8038c353a7\"}", headers: {})
stub_request(:post, "https://onesignal.com/api/v1/notifications?output=json").
with(headers: { 'Content-Type' => 'application/json' }).
to_return(status: 200, body: "{ \"id\": \"dc132d99-fcb2-4e43-9339-0e80c9f9b3b6\",\"recipients\": 1 }", headers: {})
stub_request(:get, "http://ip-api.com/json/0.0.0.0?output=json").
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => "{\"lat\": 0, \"lon\": 0}", :headers => {})
if ENV['TWILIO_USER_AGENT'].present?
stub_request(:post, "https://#{ENV['TWILIO_SID']}:#{ENV['TWILIO_AUTH_TOKEN']}@api.twilio.com/2010-04-01/Accounts/#{ENV['TWILIO_SID']}/Messages.json").
with(:body => {"Body"=>"This is a test message", "From"=>"#{ENV['TWILIO_NUMBER']}", "To"=>"+60123456789"},
:headers => {'Accept'=>'application/json', 'Accept-Charset'=>'utf-8', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>"#{ENV['TWILIO_USER_AGENT']}"}).
to_return(:status => 200, :body => "{\"success\": true,\"info\": \"SMS sent to +60123456789\",\"data\": {}}", :headers => {})
else
stub_request(:post, "https://#{ENV['TWILIO_SID']}:#{ENV['TWILIO_AUTH_TOKEN']}@api.twilio.com/2010-04-01/Accounts/#{ENV['TWILIO_SID']}/Messages.json").
with(:body => {"Body"=>"This is a test message", "From"=>"#{ENV['TWILIO_NUMBER']}", "To"=>"+60123456789"},
:headers => {'Accept'=>'application/json', 'Accept-Charset'=>'utf-8', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded'}).
to_return(:status => 200, :body => "{\"success\": true,\"info\": \"SMS sent to +60123456789\",\"data\": {}}", :headers => {})
end
stub_request(:post, %r{https://api.silverstreet.com/send.php?}).
to_return(:status => 200, :body => "01", :headers => {})
end
# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# These two settings work together to allow you to limit a spec run
# to individual examples or groups you care about by tagging them with
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
# get run.
config.filter_run :focus
config.run_all_when_everything_filtered = true
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = 'doc'
end
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random
# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# Enable only the newer, non-monkey-patching expect syntax.
# For more details, see:
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
expectations.syntax = :expect
end
# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Enable only the newer, non-monkey-patching expect syntax.
# For more details, see:
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
mocks.syntax = :expect
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended.
mocks.verify_partial_doubles = true
end
=end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment