The question title pretty much sums it up, but here's a more chronological description:
I started a new rails 3.2.9 app, did not pass any special options (ie. did not skip test unit).
I added minitest-rails to the gemfile and ran bundle install.
I deleted the contents of the test folder, and ran rails g mini_test:install.
Now if I run rake test, nothing happens.
I can make my own rakefile and specify TestTask manually, but I don't get the options to do things like rake test:controllers that are supposed to come built-in unless I manually dupe all that.
Has anyone else run into this?
Make sure you add require 'test_helper' on top of your test file. e.g.
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
test "should pass" do
assert true
end
end
The auto generated test_helper file I have looks like that:
ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
fixtures :all
end
Glad you are making the switch to MiniTest! I may be able to help you get on the right track.
Honestly, I would avoid rake entirely. Try running a test from the command line to make sure your testing suite is working.
ruby -Itest test/unit/something.rb
After you know your tests are passing then get guard-minitest and set it up to watch your files. When you save a change it will automatically run the test for you. The worst part of minitest and guard is the set up but once you get it going right you'll never want to go back.
https://github.com/guard/guard-minitest
Cheers
I guess you had not run/generate any controller or scaffold command so far.
Once you create a scaffold / controller / model and migrate the database your rake test will start working
Regarding rake test:controllers, when I tried to list out with rake -T it is not still listing
You may need to register minitest-rails as the default testing engine by adding the following to your config/application.rb file:
config.generators do |g|
g.test_framework :mini_test
end
After that, you can run controller tests with the following:
rake minitest:controllers
Related
When adding RSpec to Rails, we can finally run rails spec (or bundle exec rspec) to run the RSpec tests.
However, rails test confusingly still attempts to run the (non-existent) Minitest tests.
How can the "test" rake task be configured to run the RSpec tests instead?
You can override the test task like this:
# Add the code below in the Rakefile
require File.expand_path(‘config/application’, __dir__)
Rails.application.load_tasks
Rake::Task['test'].clear
task :test do
Rake::Task['rspec’].invoke
end
Another way (I only tried in my console with a task that puts something but it should work with rspec):
# Add the code below in the Rakefile
require File.expand_path(‘config/application’, __dir__)
Rails.application.load_tasks
task(:test).clear.enhance([‘rspec’])
Here's a layup for someone...
Back in Rails <= 4 days we'd run our test suite by simply typing $ rake at the command line, thanks to defaults in Rakefile:
task default: [:rubocop, :spec, :teaspoon]
but in Rails 5 it's not so apparent how to run default rake tasks now that rake has been replaced by rails. rails alone gives a list of possible commands rails responds to but doesn't run the specs. rails test seems logical but it tries to run minitest which we don't use. rails spec will run Rspec but not teaspoon or rubocop.
Where did this go? And why is something so apparently simple so hard for me to look up myself?
rails default
executes those tasks for me on Rails 5.2.1, though I couldn't find it documented anywhere.
Just create a new rake task that runs the other ones:
lib/tasks/my_extensions.rake
task :my_test do
Rake::Task[:foo].invoke
Rake::Task[:bar].invoke
end
# or the short version:
# task my_test: [:foo, :bar]
task :foo do
puts "FOO"
end
task :bar do
puts "BAR"
end
Run rails my_test and you will see FOO and BAR printed in your console.
If you don't know where to place the file to write the code above, check your /Rakefile:
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require_relative 'config/application'
Rails.application.load_tasks
It says to write them inside lib/tasks and end them with .rake, you don't need to require them. In your specific question, change my code from :foo and :bar to your specific tasks :rubocop :spec :teaspoon.
However, it looks like you are doing some BDD or TDD cycle. Check out rails Guard, it might help you better. I use it in my project and it works perfectly.
I'm using Rails 5.1 with Minitest and Searchkick gem and in my system tests I need to have the data indexed in ElasticSearch to make sure the tests pass.
If I add a breakpoint and inspect
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
require 'pry'; binding.pry
# Add more helper methods to be used by all tests here...
end
all my models have zero records, assuming I've recreated the database with:
rails db:drop db:create db:migrate
So how can I have the code Model.reindex running after the loading of fixtures?
Note: I could use the setup but that way I will do a reindex in all needed models before each test, increasing the time.
You can use a class variable in your setup method like this:
class SomeTest
setup do
##once ||= Model.reindex
end
end
I had the same issue on the CI server. I fixed it by preloading the test database before running the tests.
bin/rails db:fixtures:load
(Source: https://api.rubyonrails.org/v5.1.0/classes/ActiveRecord/FixtureSet.html)
I have a Rails 2.2 app that I'm supporting and one thing that needs to be done before the app start is a small rake task to initialize and make sure the database is properly set. How can I get this task run right after rspec initializes the database. I run rspec using the rails spec command.
You could put a simple system call to your spec_helper.rb file.
An Example could look like this
# run rake task
`rake your_task RAILS_ENV=test`
RSpec.configure do |config|
...
end
I'm developing a Rails application with Rspec for unit testing.
Weeks ago, Rspec used to migrate the database to the last version automatically when executing 'rake spec', but now it doesn't do it automatically, I have to implement everything for myself.
This happens in test environment, because my development data doesn't desappear.
Is my fault? I didn't change anything, I think :)
Typically what I do is use an alias command that runs both migrate and prepares the test database.
rake db:migrate && rake db:test:prepare
In your .bashrc just create an alias command like so and then run migrate_databases whenever you need to.
alias migrate_databases='rake db:migrate && rake db:test:prepare'
My solution for Rails 4:
in spec/spec_helper.rb or anywhere in testing environment initialization code:
# Automigrate if needs migration
if ActiveRecord::Migrator.needs_migration?
ActiveRecord::Migrator.migrate(File.join(Rails.root, 'db/migrate'))
end
UPD: As Dorian kindly pointed out in comments, you don't need to check separately if there are any pending migrations, because ActiveRecord::Migrator.migrate already does this behind the scenes. So you can effectively use just this one line:
ActiveRecord::Migrator.migrate(File.join(Rails.root, 'db/migrate'))
Rails 4.1 forward you can use:
ActiveRecord::Migration.maintain_test_schema!
Add at the top of your spec_helper.rb or rails_helper.rb and you're good to go. More info here.
Here's my workaround:
Rakefile:
require File.expand_path('../config/application', __FILE__)
require 'rake'
require "rspec/core/rake_task"
MyApp::Application.load_tasks
desc "Run specs"
RSpec::Core::RakeTask.new(:spec)
task :run_specs => ['db:test:clone', :spec] do
end
task :default => :run_specs
Then I run $ rake run_specs
for some reason default task doesn't default to run_specs
See if you have the following in your spec_helper.rb? Everytime you run specs, RSpec checks if there are pending migrations.
#Checks for pending migrations before tests are run.
#If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
This works even when Rails is not loaded and only does one SQL query most of the time.
if defined?(ActiveRecord::Migrator)
ActiveRecord::Migrator.migrate(File.join(Rails.root, 'db', 'migrate'))
end