Removing the default test suite from ruby on rails 4 - ruby-on-rails

When I create a rails app I usually do it like:
rails new <app> --skip-test-suite
No I have an app where I forgot to add the param --skip-test-suite and I would want to use RSPEC instead of the default test suite. How can I get rid of the default? Can I just remove the test directory and what gems would I need to get rid of?

If you are using Rails 3, to totally remove TestUnit from your application, remove this line from your config/application.rb
require "rails/test_unit/railtie"
And add this to config/application.rb
config.generators do |g|
g.test_framework :rspec
end

Related

Is possible to test before Rails loaded?

I'm creating a gem and this gem depends Rails is loaded. I need to create a scenario when Rails is not loaded. I have this code in my gem as an example:
if defined?(Rails) && Rails.application
#some code
end
I've tried to create a stub, but it seems Rails restarts the application method when we set it with nil. https://github.com/rails/rails/blob/fbe2433be6e052a1acac63c7faf287c52ed3c5ba/railties/lib/rails.rb#L38
Rails.stub(:application, nil) do
# test
end
Easy. Just setup a bootstapper file for your test that does not load Rails.
# spec/barebones_helper.rb
RSpec.configure do |config|
end
# optionally load your dependencies:
# Bundler.require(:some_group)
# spec/lib/mygem_spec.rb
require 'barebones_helper'
In fact rspec-rails already generates two separate files*:
spec_helper.rb - just configures rspec.
rails_helper.rb requires spec_helper.rb and then boots Rails through require File.expand_path('../config/environment', __dir__).
Simply changing require 'rails_helper' to require 'spec_helper' will change the test to become executed outside rails.
However be aware that running the tests through Spring will generate strange results as it will keep Rails running in the background. If you're using a spring binstub you can disable spring with the DISABLE_SPRING env var.

how to remove all test modules in an existing Rails project?

I am new to ruby and Ruby on Rails. I used command rails new [project name] to create a new rails project, and when using rails generate scaffold [Model name] to create a model, rails itself always create a testing module related to the model and the controller. so, how do i remove all rails default testing module and replace with some custom testing modules like Rspec?
If you have gem rspec-rails in your Gemfile, and don't have minitest, then rails generators already informed that you use Rspec instead of Minitest. rails generate command will create specs instead of tests.
Off-topic: it creates fixtures as well. If instead of fixtures you use factories you need to declare it to your config-file:
# config/application.rb
config.generators do |g|
g.factory_bot dir: 'spec/factories'
end

Rspec - where is the extra configuration for test environment?

I am using the default test for different purposes and I have decided to make a specific rspec environment configuration for running the test suite.
However, I discovered that upon changing to ENV["RAILS_ENV"] ||= rspec in my rails_helper.rb file, suddenly a LOT of things are going wrong, constants are not being loaded (FactoryGirl, DatabaseCleaner, etc. throw uninitialized constant errors)
My question is, where is the code that loads those guys in test environment ? Since I am planning to use this stage for other purposes than running automatic tests, I'm afraid this "out of nowhere" added configuration might not work well with what I am planning to do.
From the perspective of Rails, the test environment is configured and loaded like any other environment such as production or development. You can see this prefixing RAILS_ENV=test to many of the native Rails commands e.g. RAILS_ENV=test rails c will load the rails console for the test environment, and so on. Similarly, all test-specific configuration within Rails is defined in test.rb in your config/environments folder.
However, when you run your specs with rspec spec, you're actually starting the RSpec runner, which, for most intents and purposes, runs independently of Rails (even with the rspec-rails gem).
By convention, when RSpec starts the first thing it does is read command line args from the .rspec in the current directory, if it exists. Then it runs spec_helper.rb (and also rails_helper.rb for rspec-rails 3+). It's actually the spec_helper.rb which does all the heavy-lifting in loading the Rails environment for your tests, along with any of the modules you're using in tests, such as DatabaseCleaner, FactoryGirl, etc.
If you're wondering how RSpec hooks into Rails, the bulk of it is performed in this line, which bootstraps Rails.
require File.expand_path('../../config/environment', __FILE__)
Now, as to your question, without the ENV['RAILS_ENV'] ||= 'test' statement, the above line will load Rails in the default environment (development), which isn't what you want, since any gems not in the :test group will not be loaded, and environments/test.rb will not be loaded either.
TL;DR
Test configuration is handled by two files: spec/spec_helper.rb (sometimes named rails_helper.rb) and config/environments/test.rb. The former configures RSpec and any objects and modules which will be used specifically within the files used in spec, the latter configures your Rails app itself. Omitting ENV['RAILS_ENV'] ||= test loads the development environment and gemsets instead of the test environment and gemsets, which is why you're getting a ton of errors.
If you are getting uninitialized constant errors for FactoryGirl, DatabaseCleaner etc, you most likely included them to test group in your Gemfile.
You should move them to rspec group, eg:
# Gemfile
group :rspec do
gem 'factory_girl_rails', '~> 4.0'
gem 'faker'
end

Testing Rails 3.1 mountable engine with Rspec

I started making a Rails 3.1 engine, and I'm having a hard time testing it using rspec.
First of all, if I run rails g integration_test whatever it creates a regular integration test in tests/integration instead of spec/requests (the rspec-rails gem is installed and required as a development dependency in the gemspec file)
Also, when I run a spec test I get an error saying the table corresponding to the model I'm testing has not been created. I tried rake engine_name:install:migrations and running rake db:migrate from inside the dummy app, and I get a "table already exists" error.
Everything just seems disconnected, I feel I'm missing something here to make the rspec gem work seamlessly as it usually does with full rails applications.
I followed all the changes from here http://rubyx.com/2011/03/01/start-your-engines and I can test the engine manually by launching the dummy app via the console as shown here http://railscasts.com/episodes/277-mountable-engines.
Is there a way to make rspec the default for testing a rails 3.1 engine?
I am using RSpec with a Rails engine without issues.
I created my plugin using the following switches: -T --full --dummy-path=spec/dummy.
-T excludes test/unit
--full indicates that the plugin is an engine
--dummy-path is simply so that we don't get a test directory (the
default is test/dummy).
From there I used the spec_helper from the "start your engines" article:
# Configure Rails Envinronment
ENV["RAILS_ENV"] = "test"
require File.expand_path("../dummy/config/environment.rb", __FILE__)
require 'rspec/rails'
ENGINE_RAILS_ROOT=File.join(File.dirname(__FILE__), '../')
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[File.join(ENGINE_RAILS_ROOT, "spec/support/**/*.rb")].each {|f| require f }
RSpec.configure do |config|
config.use_transactional_fixtures = true
end
For the generators. I add a config.generators block to my engine.rb file like so:
module MyEngine
class Engine < Rails::Engine
config.generators do |g|
g.test_framework :rspec, :view_specs => false
end
end
end
With that, I'm able to get rspec tests when running a generator like the model generator.
As for the DB, is your database.yml file set up correctly? Did you load the test environment, e.g. rake db:test:clone or rake db:migrate RAILS_ENV=test? My guess is that RSpec can't see your tables because there isn't a test database set up.
I was looking for the same answer and I found the combustion gem* which promise to setup a full environment for spec'ing your engine in a simpler way. Just add
gem.add_development_dependency 'combustion', '~> 0.3.1'
to your gemspec and run
bundle exec combust
to reproduce a full rails app in your spec directory.
*I haven't tried it yet...

Remove ActiveRecord in Rails 3

Now that Rails 3 beta is out, I thought I'd have a look at rewriting an app I have just started work on in Rails 3 beta, both to get a feel for it and get a bit of a head-start. The app uses MongoDB and MongoMapper for all of its models and therefore has no need for ActiveRecord. In the previous version, I am unloading activerecord in the following way:
config.frameworks -= [ :active_record ] # inside environment.rb
In the latest version this does not work - it just throws an error:
/Library/Ruby/Gems/1.8/gems/railties-3.0.0.beta/lib/rails/configuration.rb:126:in
`frameworks': config.frameworks in no longer supported. See the generated
config/boot.rb for steps on how to limit the frameworks that will be loaded
(RuntimeError)
from *snip*
Of course, I have looked at the boot.rb as it suggested, but as far as I can see, there is no clue here as to how I might go about unloading AR. The reason I need to do this is because not only is it silly to be loading something I don't want, but it is complaining about its inability to make a DB connection even when I try to run a generator for a controller. This is because I've wiped database.yml and replaced it with connection details for MongoDB in order to use this gist for using database.yml for MongoDB connection details. Not sure why it needs to be able to initiate a DB connection at all just to generate a controller anyway....
Is anyone aware of the correct Rails 3 way of doing this?
I'm going by this from reading the source, so let me know if it actually worked. :)
The rails command that generates the application template now has an option -O, which tells it to skip ActiveRecord.
If you don't feel like rerunning rails, you should check the following in your existing app:
Check that your config/application.rb doesn't have require 'rails/all' or require "active_record/railtie". Instead, for a standard Rails setup without ActiveRecord, it should have only the following requires:
require File.expand_path('../boot', __FILE__)
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
require "sprockets/railtie"
# Auto-require default libraries and those for the current Rails environment.
Bundler.require :default, Rails.env
If, in config/application.rb, you are using the config.generators section, make sure it doesn't have the line g.orm :active_record. You can set this explicitly to nil, if you want, but this should be the default when g.orm is completely omitted.
Optional, but in your Gemfile, remove the gem line that loads the module for your database. This could be the line gem "mysql" for example.
Rails 4
I was looking for how to disable it in rails 4 and only found this answer which no longer works in rails 4. So this is how you can do it in rails 4 (tested in RC1).
In a new project
rails new YourProject --skip-active-record
In an existing project
In your Gemfile, remove the database driver gem, e.g. gem 'sqlite3' or gem 'pg'.
In config/application.rb, replace require 'rails/all' with
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"
In config/environments/development.rb, remove or comment out config.active_record.migration_error = :page_load
Potentially you have to remove active_record helpers from the spec_helper (via VenoM in the comments)
Potentially you have to remove the ConnectionManagement middleware (seems to be the case with unicorn): config.app_middleware.delete "ActiveRecord::ConnectionAdapters::ConnectionManagement" (via https://stackoverflow.com/a/18087332/764342)
I hope this helps others looking for how to disable ActiveRecord in Rails 4.
For a new rails app, you can have it exclude active record by specifying the --skip-active-record parameter. Eg:
rails new appname --skip-active-record
If you generated a new project using Rails 3.2, you will also need to comment out:
config.active_record.mass_assignment_sanitizer = :strict
and
config.active_record.auto_explain_threshold_in_seconds = 0.5
in your development.rb file.
All of the above are true. The one more thing which I had to do in rails 3.1 is to comment out
config.active_record.identity_map = true
in config/application.rb.
If you're running rspec, you also need to remove (in spec_helper):
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
and remove
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
Also comment out
# config/application.rb
config.active_record.whitelist_attributes = true
(noted on rails 3.2.13)

Resources