Jets Testing
RSpec configurations and utils for usage in gems, engines and apps.
Installation
Add this line to your application's Gemfile:
gem "jets-testing"
or to your engine's Gemfile and .gemspec
:
# Gemfile
gem "jets-testing"
# .gemspec
s.add_development_dependency "jets-rubocop"
Usage
This gem provides two configurations, which could be loaded via:
-
require "jets/testing/rspec_configuration"
– this is RSpec core configuration, which must be added to yourspec_helper.rb
; it doesn't contain anything Rails specific -
require "jets/testing/rails_configuration" – typical RSpec config for Rails, which includes a lot of useful stuff (see below); require it in your
rails_helper.rb`.
Tools
Here is the list of gems included into Rails config:
- rspec-rails
- shared-factory
- isolator
- n_plus_one_control (verbose by default)
- shoulda-matchers
- test-prof
- ActiveSupport::Testing::TimeHelpers
You can also add a dummy Rails app (in case of gem/engine) using combustion
gem, which is included, too. For example:
require "jets/testing/rails_configuration"
require "combustion"
Combustion.initialize! :action_controller, :active_record, :active_job, :action_mailer do
config.logger = Logger.new(nil)
config.log_level = :fatal
config.active_record.raise_in_transactional_callbacks = true
config.active_job.queue_adapter = :test
end
Additional configuration could be added under spec/internal
.
Helpers
-
fixture_file_path(*args)
– returns path to a file fixture (based onfixture_path
) -
mails_for(email_or_user)
- returns a proxy for user's emails; useful to test that an email has been sent and it's contents, for example:
mailbox = mails_for(user) # or mails_for(email)
expect { subject }.to change(mailbox, :size).by(1)
expect(mailbox.last.subject).to eq "Hey, you got an email!"
Shared Contexts
-
"active_job:perform"
(active_job: :perform
) – perform enqueued Active Job jobs automatically -
"csrf:on"
(csrf: :on
) – turn on CSRF protection forActionController::Base
(it's off by default in specs) -
"shared:request"
(type: :request
, i.e. included automatically) – addjson_response
method and declaresubject
for request specs (see testing guide).