RSpec + Rails Without rspec-rails @ RoRoSyd November 2015

D79fc498d7a5b2ce12180890247476f0?s=47 Jon Rowe
November 10, 2015

RSpec + Rails Without rspec-rails @ RoRoSyd November 2015


Jon Rowe

November 10, 2015


  1. 6.

    Autoloading To autoload or not to autoload. `require` works… but

    can be painful Rails constant lookup can be surprising
  2. 7.

    require 'bundler/setup' gem_paths = Dir[ File.join Bundler.bundle_path, “/gems/**/lib” ] app_paths

    = Dir[ File.expand_path(‘./lib/**'), File.expand_path(‘./app/**') ] (gem_paths + app_paths).each do |path| ActiveSupport::Dependencies .autoload_paths << File.expand_path(path) end ActiveSupport::Dependencies.hook!
  3. 10.

    module Support::Database def self.setup! return true if defined?(@connection) require ‘yaml’;

    require ‘active_record' config = YAML.load(‘config/database.yml') ).result) @connection = ActiveRecord::Base .establish_connection(config[‘test']) ActiveRecord::Base .raise_in_transactional_callbacks = true true end end RSpec.configuration.before :context, :db do Support::Database.setup! end
  4. 11.

    RSpec.describe 'MyModel', :db do let(:models) do (1..6).map { |i| MyModel.create!

    hidden: false } end describe '.active' do it 'returns all active models' do models[0..2].each do |model| model.update_attributes! hidden: tru end expect( ).to match_array models[3..-1] end end end
  5. 12.

    # For verified doubles with ActiveRecord ::RSpec::Mocks.configuration .when_declaring_verifying_double do |possible|

    target = if target.respond_to?(:define_attribute_methods) end end
  6. 15.

    Controller Specs Controller specs are already fullstack Too hard to

    isolate them Refactor logic to services and test or… Write acceptance tests
  7. 17.

    require 'capybara/dsl' RSpec.configure do |config| config.include Capybara::DSL, :app config.before :context,

    :app do unless require ‘config/environment.rb’ = Rails.application end Capybara.asset_host = "http://localhost:#{ENV['PORT'] || '8080'}" end config.after :example, :app do Capybara.reset_sessions! end end
  8. 18.

    RSpec.describe 'Downloading a report', :app, :db do include Support::AdminHelper before

    do sign_in_as a_valid_admin end context 'when statistics are present' do it 'produces a csv' do visit "/admin" click_link 'Download CSV' expect { CSV.parse body }.to_not raise_error CSV::ParseError end end end
  9. 19.

    RSpec.configure do |config| config.register_ordering :global do |examples| acceptance, unit =

    examples.partition do |ex| ex.metadata[:acceptance] end unit.shuffle + acceptance.shuffle end end
  10. 20.

    Other Services / Presenters / Forms / ETC As long

    as they’re PORO(ish), work fine Mailers work with `email_spec`
  11. 21.