.TH "COMBUSTION" "1" "November 2022" .SH "NAME" \fBCombustion\fR .P Combustion is a library to help you test your Rails Engines in a simple and effective manner, instead of creating a full Rails application in your spec or test folder\. .P It allows you to write your specs within the context of your engine, using only the parts of a Rails app you need\. .SH Usage .P Get the gem into either your gemspec or your Gemfile, depending on how you manage your engine's dependencies: .RS 2 .nf # gemspec gem\.add_development_dependency 'combustion', '~> 1\.3' # Gemfile gem 'combustion', '~> 1\.3' .fi .RE .P In your \fBspec_helper\.rb\fP, get Combustion to set itself up \- which has to happen before you introduce \fBrspec/rails\fP and \- if being used \- \fBcapybara/rails\fP\|\. Here's an example within context: .RS 2 .nf require 'bundler' Bundler\.require :default, :development # If you're using all parts of Rails: Combustion\.initialize! :all # Or, load just what you need: # Combustion\.initialize! :active_record, :action_controller require 'rspec/rails' # If you're using Capybara: # require 'capybara/rails' RSpec\.configure do |config| config\.use_transactional_fixtures = true end .fi .RE .P Please note that using \fB:all\fP as an argument for \fBCombustion\.initialize!\fP will load all key parts of Rails that are considered essential for that version\. For example: ActiveJob is only loaded for Rails 4\.2 onwards, and Sprockets is only loaded for Rails 3\.1\-6\.1 (as it is no longer part of the default set in 7\.0)\. .P You'll also want to run the generator that creates a minimal set of files expected by Rails \- run this in the directory of your engine: .RS 2 .nf combust # or, if bundling with the git repo: bundle exec combust .fi .RE .P Minitest support is considered to be experimental, but it's certainly working .UR https://github.com/pat/combustion/issues/78 .I for some .UE . Comments on others' experiences are welcome! .P What Combustion is doing is setting up a Rails application at \fBspec/internal\fP \- but you only need to add the files within that directory that you're going to use\. Read on for some detail about what that involves\. .P If you want to use Cucumber, I recommend starting with .UR https://github.com/pat/combustion/issues/16 .I these notes in issue #16 .UE from Niklas Cathor\. .SS Configuring a different test app directory .P If you want your app to be located somewhere other than \fBspec/internal\fP, then make sure you configure it before you call \fBCombustion\.initialize!\fP: .RS 2 .nf Combustion\.path = 'spec/dummy' Combustion\.initialize! :all .fi .RE .SS Configuring which Rails modules should be loaded\. .P By default, Combustion doesn't come with any of the Rails stack\. You can customise this though \- just pass in what you'd like loaded to the \fBCombustion\.initialize!\fP call: .RS 2 .nf Combustion\.initialize! :active_record, :action_controller, :action_view, :sprockets .fi .RE .P And then in your engine's Gemfile: .RS 2 .nf group :test do gem 'activerecord' gem 'actionpack' # action_controller, action_view gem 'sprockets' end .fi .RE .P Make sure to specify the appropriate version that you want to use\. .P ActiveSupport and Railties are always loaded, as they're an integral part of Rails\. .SS Using Models and ActiveRecord .P If you're using ActiveRecord, then there are two critical files within your internal Rails app at \fBspec/internal\fP that you'll need to modify: .RS 1 .IP \(bu 2 config/database\.yml .IP \(bu 2 db/schema\.rb .RE .P Both follow the same structure as in any normal Rails application \- and the schema file lets you avoid migrations, as it gets run whenever the test suite starts\. Here's a quick sample (note that tables are overwritten if they already exist \- this is necessary): .RS 2 .nf ActiveRecord::Schema\.define do create_table(:pages, :force => true) do |t| t\.string :name t\.text :content t\.timestamps end end .fi .RE .SS Disabling Database Preparation .P If you are preparing your own database manually or through different processes, you can disable different parts of the setup process by the following flags: \fB:database_reset\fP, \fB:load_schema\fP, and \fB:database_migrate\fP\|\. All default to true\. .RS 2 .nf Combustion\.initialize! :active_record, :database_reset => false, :load_schema => false .fi .RE .SS Configuring Combustion to initialise the test db from a \.sql file instead of schema\.rb .P Name the file structure\.sql and configure Combustion to use it before initialising: .RS 2 .nf Combustion\.schema_format = :sql Combustion\.initialize! :all .fi .RE .P Any models that aren't provided by your engine should be located at \fBspec/internal/app/models\fP\|\. .SS Using ActionController and ActionView .P You'll only need to add controllers and views to your internal Rails app for whatever you're testing that your engine doesn't provide \- this may be nothing at all, so perhaps you don't even need \fBspec/internal/app/views\fP or \fBspec/internal/app/controllers\fP directories\. .P However, if you're doing any testing of your engine's controllers or views, then you're going to need routes set up for them \- so modify \fBspec/internal/config/routes\.rb\fP accordingly: .RS 2 .nf Rails\.application\.routes\.draw do resources :pages end .fi .RE .P Just like in a standard Rails app, if you have a mounted engine, then its routes are accessible through whatever it has been loaded as\. .SS Customizing Rails application settings .P If you would like to specify any Rails configuration parameter, you can do it without creating any environment file, simply passing a block to Combustion\.initialize! like this: .RS 2 .nf Combustion\.initialize! :all do config\.active_record\.whitelist_attributes = false end .fi .RE .P Values given through the initialize! block will be set during Rails initialization process, exactly before the corresponding environment file inside \fBspec/internals/config/enviroments\fP is loaded (when that file exists), overriding Combustion's defaults\. .P Parameters defined in, for instance, \fBspec/internals/config/environments/test\.rb\fP, would override Combustion's defaults and also config settings passed to initialize!\. .SS Using other Rails\-focused libraries .P Be aware that other gems may require parts of Rails when they're loaded, and this could cause some issues with Combustion's own setup\. You may need to manage the loading yourself by setting \fB:require\fP to false in your Gemfile for the gem in question, and then requiring it manually in your spec_helper\. View .UR https://github.com/pat/combustion/issues/33 .I issue #33 .UE for an example with FactoryBot\. .SS Environment and Logging .P Your tests will execute within the test environment for the internal Rails app \- and so logs are available at \fBspec/internal/log/test\.log\fP\|\. You should probably create that log directory so Rails doesn't complain\. .SS Rack it up .P Once you've got this set up, you can fire up your test environment quite easily with Rack \- a \fBconfig\.ru\fP file is provided by the generator\. Just run \fBrackup\fP and visit http://localhost:9292 .SS Get your test on! .P Now you're good to go \- you can write specs within your engine's spec directory just like you were testing a full Rails application \- models in \fBspec/models\fP, controllers in \fBspec/controllers\fP\|\. If you bring Capybara into the mix, then the standard helpers from that will be loaded as well\. .RS 2 .nf require 'spec_helper' describe Page do describe '#valid' do it 'requires a name' do # This is just an example\. Go write your own tests! end end end .fi .RE .SH Compatibility .P The current test matrix covers MRI 2\.4 to 3\.1, and Rails 3\.1 to 7\.0\. It will possibly work on older versions and other Ruby implementations as well\. .P You can also use Combustion with multiple versions of Rails to test compatibility across them\. .UR https://github.com/thoughtbot/appraisal .I Appraisal .UE is a gem that can help with this, and a good starting reference is the .UR https://github.com/pat/thinking-sphinx .I Thinking Sphinx .UE test suite, which runs against .UR https://github.com/pat/thinking-sphinx/blob/master/Appraisals .I multiple versions .UE of Rails\. .SH Limitations and Known Issues .P Combustion is currently written with the expectation it'll be used with RSpec, but others have got it working with .UR https://github.com/pat/combustion/issues/78 .I Minitest .UE . I'd love to make this more flexible \- if you want to give it a shot before I get around to it, patches are very much welcome\. .P I've not tried using this with Cucumber, but it should work in theory without too much hassle\. Let me know if I'm wrong! .SH Contributing .P Please note that this project now has a .UR http://contributor-covenant.org/version/1/0/0/ .I Contributor Code of Conduct .UE . By participating in this project you agree to abide by its terms\. .P Contributions are very much welcome \- but keep in mind the following: .RS 1 .IP \(bu 2 Keep patches in a separate branch .IP \(bu 2 Don't mess with the version or history file\. I'll take care of that when the patch is merged in\. .RE .P The tests are extremely minimal, and patches to extend the suite are especially welcome\. .SH Credits .P Copyright (c) 2011\-2021, Combustion is developed and maintained by Pat Allan, and is released under the open MIT Licence\. Many thanks to HyperTiny for encouraging its development, and .UR https://github.com/pat/combustion/contributors .I all who have contributed patches .UE .