How to run only controller tests in MiniTest? - ruby-on-rails

What is the rake command for running only controller tests in minitest?
rake test:controller doesn't do the trick.

Try making it plural. This is the typical command:
rake test:controllers
http://guides.rubyonrails.org/testing.html#rake-tasks-for-running-your-tests
Section 6 covers the rake commands for testing.

Has to be plural rake test:controllers as you're running all of them.
Please take a look at http://guides.rubyonrails.org/testing.html#rake-tasks-for-running-your-tests for more rake commands.
If you want to run a specific file, then use the TEST argument:
rake test TEST=test/controllers/application_controller_test.rb

As an update:
rails test path/to/test/file.rb has become my go-to solution.
rails test path/to/test/file.rb:123 lets you pick the test via line number as well.

Related

Rails 5.1 run system tests and normal tests with one command

In Rails 5.1, you can do bin/rails test to run normal tests, and bin/rails test:system. What is the Rails sanctioned way of running both at the same time?
bin/rails test:system test
Specifying test:system before test will run both system and ordinary tests. The opposite order will only run the ordinary tests however.
rails test:all (Rails 6.1+)
Rails 6.1 introduces a new command - rails test:all.
It runs all test files in the test directory, including system tests.
Here is a link to PR.
And also a link to the docs (please, scroll down to yellow box).
In case anyone else is looking for the answer:
bin/rails test test/*
If it is your intention to run it using just $ rake or $rake test you can add into your Rakefile:
task test: 'test:system'
This will makes 'test:system' a "prerequisites" for "test" task
At least from the official rails guide, it seems there is no way of doing it:
By default, running bin/rails test won't run your system tests. Make sure to run bin/rails test:system to actually run them.
Ref: rails guide
You can also add this snippet in your lib/tasks folder, that will give you the option to do rake test:all
namespace :test do
desc "Run both regular tests and system tests"
task :all => 'test' do
Minitest.after_run {system('rake test:system')}
end
end
Summary of all the answers for easy reference:
System tests Only
bin/rails test:system
Ordinary tests Only
bin/rails test .
ALL tests
bin/rails test:all

Rails - rspec - How can I run all the integration tests, similar to rake spec:models

The following commands work:
rake spec:models
rake spec:controllers
rake spec:requests
but the following does not:
rake spec:integration
rake spec:integrations
How can I run all the integration tests alone?
One option is to use rspec spec/integration/* I guess.
I was hoping for an answer that worked with rake like the other methods.
Actually, request specs are integration tests in rspec. Were some of the tests inherited from test::unit?
Test::Unit is the default test library for a new rails application created using the generator (unless -T is specified which skips the creation of test::unit files), so it will come with spec/integration, whereas for Rspec, the convention is to use spec/requests. In fact if you try to generate a new integration_test now by rails g integration_test testname, that will go into requests.

How do I run a ruby script, that I put in my /lib/tasks/ directory in my Rails app, once?

Eventually I would like to get to setting it up as a Rake task and do a cron job, but for right now...all I want to do is take my ruby script that used to work as a standalone script and have it work within my Rails app.
I renamed the file to be .rake instead of .rb and tried doing rake my_script at the command-line, but that gave me this error message:
rake aborted!
Don't know how to build task 'my_script'
(See full trace by running task with --trace)
How do I run this script within my Rails environment?
This is the first time I am doing something like this, so any assistance would be greatly appreciated.
Thanks.
I think what you're looking for is rails runner. I know in Rails 2.3.x you'd do
ruby script/runner <your file>
In Rails 3 it might be slightly different.
http://guides.rubyonrails.org/command_line.html#rails-runner
The primary difference between a runner and a rake task is : runner would boot rails while rake task doesn't (you can tell it to do so).
Since rake can do both (boot/no boot), there's no concept of runner in rails-3 anymore.
So, create a rake task: whatever_name.rake
Example:
desc "This task does awesome stuff"
task :do_awesome_stuff do
awesome_method
end
def awesome_method
#put your ruby code here
end
Now from your command prompt, type rake do_awesome_stuff to execute this rake task.
To make it boot Rails, change task definition to this:
task :do_awesome_stuff => :environment do

RSpec > Is there a way to run all tests with one command?

Is there a command that does this? I've searched but can not find anything
Try this in your terminal window:
bundle exec rspec spec
if you are using rspec-rails then you can run using rake spec
if you're testing models, use rake spec:models (or rake spec:routing or rake spec:controllers)
if just testing one model, use rake SPEC=app/models/modelname.rb
Create a .rspec file in the root of your project
Add the path to where your specs are e.g.
--default-path test/spec/
Add the pattern of how your files are named e.g.
--pattern ****/*.spec
Run rspec and it should pick all your specs and run them :)
default-path and pattern are simply command line arguments, which means you can also add any other command line argument that rspec takes (can run rspec --help to see the available options)
According to https://www.relishapp.com/rspec/rspec-core/v/2-0/docs/configuration/read-command-line-configuration-options-from-files you can also put the options in ~/.rspec, which will make the options available globally. Local .rspec file options will override the global options.
For controller test
bundle exec rake spec:controllers
For model test
bundle exec rake spec:models
For all test
bundle exec rake spec
For specific file test do
rspec file_name_spec.rb (example rspec spec/controllers/groups_controller_spec.rb)
go to your app directory and run rspec spec or bundle exec rspec spec.
use spork to speed your testing...(i kinda say its compulsory)

Do I have to run rake db:test:load each time manually before runnings tests?

I'm new to Ruby on Rails.
I'm trying to set up a simple WebApp via Scaffolding. And using RSpec for my tests. Now after the scaffold command:
rails generate scaffold VideoSegment file_path:string short_name:string description:string
I ran rake db:migrate, but thats clear, bringing the data to my development database.
But the tests where not green before I did:
rake db:test:load
To bring the schema of my development database to the test database. Isn't there a way to automate this step? Or do I have to load test database again after each scaffold?
PS: Of course I know Scaffold is not doing the finest things, but for my proof of concept need it's sufficient.
Thanks for any suggestions.
Whenever you run rspec it will prepare the test schema for you using the task: db:test:prepare
So after generating migrations you have to do rake db:migrate to update the development schema and then run you spec which will automatically prepare the test database for you.

Resources