Farmbot-Web-App/spec/spec_helper.rb

178 lines
4.1 KiB
Ruby
Raw Permalink Normal View History

2019-03-11 18:35:53 -06:00
DO_INTEGRATION = !!ENV["RUN_CAPYBARA"]
ENV["MQTT_HOST"] = "blooper.io"
2019-03-11 18:35:53 -06:00
ENV["OS_UPDATE_SERVER"] = "http://example-server.com"
2018-09-27 13:48:41 -06:00
# require "deep_cover/builtin_takeover"
require "simplecov"
#Ignore anything with the word "spec" in it. No need to test your tests.
2014-06-20 19:54:45 -06:00
SimpleCov.start do
add_filter "/spec/"
add_filter "config/initializers"
2014-06-20 19:54:45 -06:00
end
2019-03-11 18:35:53 -06:00
SimpleCov.coverage_dir("coverage_api")
2017-07-27 08:51:35 -06:00
require "codecov"
SimpleCov.formatters = SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::HTMLFormatter,
SimpleCov::Formatter::Codecov,
])
require "pry"
ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)
2018-04-26 16:16:25 -06:00
# This is a stub for BunnyRB because we don't want the test suite to connect to
# AMQP for real.
2018-04-26 16:57:37 -06:00
class FakeTransport < Transport
2018-04-26 17:13:24 -06:00
# Theses are the "real" I/O inducing methods that must be Stubbed out.
MOCKED_METHODS = [:bind, :create_channel, :publish, :queue,
:send_demo_token_to, :subscribe, :topic]
2018-04-26 16:16:25 -06:00
2018-04-26 16:57:37 -06:00
# When you call an AMQP I/O method, instead of doing real I/O, it will deposit
# the call into the @calls dictionary for observation.
attr_reader :calls
2018-04-26 16:16:25 -06:00
MOCKED_METHODS.map do |name|
# Eval is Evil, but this is pretty quick for testing.
2019-03-11 18:35:53 -06:00
eval "def #{name}(*x)
key = #{name.inspect}
(@calls[key] ||= []).push(x)
@calls[key] = @calls[key].last(10)
self
end"
end
2018-04-26 16:57:37 -06:00
def initialize(*)
self.clear!
end
def start
self
end
def clear!
@calls = {}
end
end
2018-04-26 16:57:37 -06:00
Transport.default_amqp_adapter = FakeTransport
Transport.current = Transport.default_amqp_adapter.new
require "rspec/rails"
require_relative "./stuff"
require_relative "./fake_sequence"
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
2015-06-10 05:56:35 -06:00
SmarfDoc.config do |c|
c.template_file = "api_docs.md.erb"
2019-03-11 18:35:53 -06:00
c.output_file = "api_docs.md"
2015-06-10 05:56:35 -06:00
end
require "database_cleaner"
DatabaseCleaner.strategy = :truncation
# then, whenever you need to clean the DB
DatabaseCleaner.clean
2019-10-04 08:59:23 -06:00
Rails.cache.redis.flushdb
RSpec.configure do |config|
2017-03-08 07:20:11 -07:00
config.color = true
2019-05-22 14:19:26 -06:00
# config.fail_fast = 10
2016-08-31 12:16:03 -06:00
config.backtrace_exclusion_patterns = [/gems/]
config.filter_run_excluding type: :feature unless DO_INTEGRATION
2014-05-08 08:02:51 -06:00
config.include Helpers
2014-06-30 07:19:59 -06:00
config.infer_spec_type_from_file_location!
config.order = "random"
if ENV["DOCS"]
2016-12-07 12:09:38 -07:00
config.after(:each, type: :controller) do
2019-12-18 08:22:44 -07:00
if request.path.length > 0 || response.body.length > 0
SmarfDoc.run!(NiceResponse.new(request), response)
end
2015-06-10 05:56:35 -06:00
end
2016-12-07 09:52:43 -07:00
config.after(:suite) do
SmarfDoc.finish!
end
end
end
2016-12-07 12:09:38 -07:00
FAKE_ATTACHMENT_URL = "https://cdn.shopify.com/s/files/1/2040/0" \
"289/files/FarmBot.io_Trimmed_Logo_Gray_o" \
"n_Transparent_1_434x200.png?v=1525220371"
2017-09-29 08:18:27 -06:00
def run_jobs_now
delay_jobs = Delayed::Worker.delay_jobs
Delayed::Worker.delay_jobs = false
yield
Delayed::Worker.delay_jobs = delay_jobs
end
2017-03-08 07:41:51 -07:00
# Reassign constants without getting a bunch of warnings to STDOUT.
# This is just for testing purposes, so NBD.
def const_reassign(target, const, value)
2019-07-26 07:49:53 -06:00
b4 = target.const_get(const)
2017-03-08 07:41:51 -07:00
target.send(:remove_const, const)
target.const_set(const, value)
2019-07-26 07:49:53 -06:00
if block_given?
yield
target.send(:remove_const, const)
target.const_set(const, b4)
end
end
class StubResp
attr_accessor :code, :body
def initialize(code, body)
@code, @body = code, body
end
end
class NiceResponse
attr_reader :r, :body
def initialize(r)
2019-03-11 18:35:53 -06:00
@r = r
@body = r.body.read
end
def path
r.path
end
def pretty_url
r.method + " " + r.path.first(45) + query
end
def has_params?
r.params
.except(:controller, :action, :format, :id)
.keys
.length > 0
end
def has_body?
r.body.size > 4
end
def display_body
begin
JSON
.pretty_generate(JSON.parse(body))
.first(500)
rescue
JSON.pretty_generate(r
.params
.except(:controller, :action, :format, :id, :user_id, :device_id)).first(500)
end
end
def query
if r.query_string.present?
"?" + r.query_string.first(45)
else
""
end
end
end
2019-10-24 08:01:51 -06:00
FakeDeliveryInfo = Struct.new(:routing_key, :device)