how to rails test all cases together like a test suite - ruby-on-rails

I have few test case files in test/functional and they all need to be executed seperately.
ruby -Itest test/functional/abc.rb
It takes 30 secs just for rails to load and 5 secs to do tests.
Is there a way to just load rails once and run all the case files in that session?
This is a very bad working solution as it is loading rails 5 times.
find test/functional -name '*.rb'|xargs -L 1 ruby -Itest
I need a solution for old rails 3.2 with ruby 1.9.
Just to try I had written all.rb but I guess constructors need args so it did not work.
require 'test_helper'
require 'functional/api_controller_test.rb'
ApiControllerTest.new()

Make sure you name the test files with that ending: *_test.rb
In Rails 3 you can run the whole testsuite like the following:
-
$ rake test
or
$ bundle exec rake test
Read more: http://guides.rubyonrails.org/v3.2/testing.html#rake-tasks-for-running-your-tests

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

Send ActionMailer from external ruby file?

i want to make a cronjob which execute a specific ruby file in the script directory of my rails app. how can i achieve, that i can execute an actionmailer to use deliver? how do i get this mailer into this ruby file?
thanks!
Skipping the part about creating a task in crontab, here is what you can do:
Create a rake task, in lib/tasks, which sends that email and invoke the rake task from your cron job. I have done this and this works, pretty well for me.
Load the Rails environment explicitly in your ruby script. I do this in some daemons.
You can do something like this :
require File.dirname(__FILE__) + "/../config/application"
Rails.application.require_environment!
Use the rails runner. rails runner -h will give you the necessary information

script/runner in rails 3

I have 2 jobs I would like to run and they are dependant on Models in my rails application.
I added the ruby files in a separate folder called Jobs, that I have appended to the rail project.
Whenever I try to run them via ruby command I get the following error:
uninitialized constant Feedback (NameError).
Feedback here is a model I'm using in my rails app.
My questions: because the jobs I'm using are actually compatible with the script/runner command of rails 2, is there an alternative with Rails 3? If not how can I write ruby programs that depend on models I have in a rails app without getting the error I mentioned above.
Use rails runner
$ rails -h
Usage: rails COMMAND [ARGS]
...
runner Run a piece of code in the application environment
All commands can be run with -h for more information.
The "Rails 3 way" to do this is with Rake using the :environment prerequisite, which loads the Rails environment. Like so:
task :name => :environment do |t|
# actions
end
In the block you can load and execute your jobs.
If you haven't written Rake scripts before, here's a good tutorial. It's quite easy.

running tests via the terminal and rake, load test_helper

I am having a problem running my tests from the terminal and from rake, e.g. rake test:integration
At the moment, I have the requires for test_helper.rb specified like this:
require File.dirname(__FILE__) + '/../test_helper'
This works fine when running them from the terminal but obviously when it is ran from rake, the directory is different and the process cannot find the test_helper file.
I think I want to add to this to my $load_path but I am not sure how to add it when running only in the test environment.
Can anyone help me out?
You can revert to just require 'test_helper' (the default for integration tests, at least with Rails 2.3.x). This will allow tests to run from a rake task, and as long as you cd to the test directory within your rails app, you can run tests via the terminal with ruby integration/your_test.rb.

How to run Rails console in the test environment and load test_helper.rb?

The background: I'm having some problems with Thoughtbot's "Factory Girl" gem, with is used to create objects to use in unit and other tests. I'd like to go to the console and run different Factory Girl calls to check out what's happening. For example, I'd like to go in there are do...
>> Factory(:user).inspect
I know that you can run the console in different environments...
$ script/console RAILS_ENV=test
But when I do that, Factory class is not available. It looks as though test_helper.rb is not getting loaded.
I tried various require calls including one with the absolute path to test_helper.rb but they fail similarly to this:
$ script/console RAILS_ENV=test
>> require '/Users/ethan/project/contactdb/test/test_helper.rb'
Errno::ENOENT: No such file or directory -
/Users/ethan/project/contactdb/config/environments/RAILS_ENV=test.rb
Grr. Argh.
For Rails < 3.0
Run script/console --help. You'll notice that the syntax is script/console [environment], which in your case is script/console test.
I'm not sure if you have to require the test helper or if the test environment does that for you, but with that command you should at least be able to boot successfully into the test env.
As a sidenote: It is indeed kind of odd that the various binaries in script/ has different ways of setting the rails environment.
For Rails 3 and 4
Run rails c test. Prepend bundle exec if you need this for the current app environment.
For Rails 5 and 6
Run rails console -e test.
In Rails 3, just do rails console test or rails console production or rails console development (which is the default).
For Rails 5.2.0: "Passing the environment's name as a regular argument is deprecated and will be removed in the next Rails version. Please, use the -e option instead."
rails c -e test
script/console test
Should be all you need.
You can specify the environment in which the console command should operate.
rails c [environment]
Examples
1) For Staging
rails c staging
2) For Production
rails c production
For source & detailed description: The Rails Command Line
David Smith is correct, just do
script/console test
The help command will show why this works:
$ script/console -h
Usage: console [environment] [options]
-s, --sandbox Rollback database modifications on exit.
--irb=[irb] Invoke a different irb.
--debugger Enable ruby-debugging for the console.
It's the [environment] bit.
I share the asker's pain. There are really three separate questions here, some of which are addressed, some not:
How do you start the console in the test environment?
For recent Rails versions, bundle exec rails c test, or alternative syntaxes for that.
How do you ensure that test/test_helper.rb is loaded into that console session?
Something like require './test/test_helper' ought to do it.
For me, this returns true, indicating that it was not already loaded when I started the console. If that statement returns false, then you just wasted a few keystrokes, but you're still good to go.
Once test_helper is loaded, how do you call the methods defined in it?
In a typical test_helper, the custom methods are typically defined as instance methods of ActiveSupport::TestCase. So if you want to call one of them, you need an instance of that class. By trial and error, ActiveSupport::TestCase.new has one required parameter, so...pass it something.
If your test_helper has a method called create_user, you could invoke it this way:
ActiveSupport::TestCase.new("no idea what this is for").create_user
Make sure you installed the GEM and you added the following line either in your environment.rb or test.rb file.
config.gem "thoughtbot-factory_girl", :lib => "factory_girl", :source => "http://gems.github.com"
Test Env
rails console test # or just rails c test
Developement Env
rails console # or just rails c
Command to run rails console test environment is
rails c -e test
or
RAILS_ENV=test rails c
if you are facing problem something like
ActiveRecord::StatementInvalid:
Mysql2::Error: Table 'DB_test.users' doesn't exist: SHOW FULL FIELDS FROM `users`
then you should first prepare your test DB by running
bundle exec rake db:test:prepare

Resources