Rails ActiveSupport::TestCase - colourful output - ruby-on-rails

Surprisingly, I didn't find the standard way to make the output of Rails Minitest colorful. There're some workarounds, though.
So what's the way to do that?

Add minitest-rg to the test group in your Gemfile:
group :test do
gem "minitest-rg"
end
Then require minitest/rg in your test/test_helper.rb:
require "minitest/rg"
Now you have colorful test output when running rake test.

Sure, in this rakefile change it to:
namespace :test do
task :isolated do
Dir.glob("test/**/*_test.rb").all? do |file|
sh(Gem.ruby, '-w', '-Ilib:test', file, '-p')
end or raise "Failures"
end
end
Then from the console run rake test:isolated while in activesupport folder and watch the rainbow go! This is using minitests built in pride library for color.

Related

Require a gem in all migrations

I have this gem in my gemfile (gem "lhm", "~> 2.2.0", require: false) that I want required in all migrations. Behavior similar to that of requiring spec_helper in all spec files.
I considered doing something with bin/rails or bin/rake but I don't want it required in all tasks, just migrations. Ie. rake db:migrate or the now alias rails db:migrate
As you probably don't have all the migrations using lhm I would suggest adding require "lhm" only to the migrations you're using. But to answer your question you could use rake's enhance method:
add the below to lib/tasks/something.rake:
namespace :load do
namespace :lhm do
desc "This just loads lhm"
task lhm: :environment do
require "lhm"
end
end
end
Rake::Task['db:migrate'].enhance(['load:lhm'])
As an example see here

Heroku could not detect rake tasks (LoadError: cannot load such file -- rspec/core/rake_task)

I'm using travisCI to deploy to heroku and I am getting this error. It has only just started happening.
I have the basic rails Rakefile and I have a file that looks like this as otherwise travis cannot detect the rake tasks:
# lib\tasks\spec.rake
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new
task :default => :spec
Why would this error be displaying specifically for heroku?
EDIT - I had a similar version to the (better) answer given:
begin
require 'rspec/core/rake_task'
desc "Run all examples"
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = %w[--color]
t.pattern = 'spec/*_spec.rb'
end
rescue LoadError
end
If rspec isn't in the production group (it generally isn't) then the code you posted would fail when run in a production environment like heroku.
In the rspec docs they recommend doing this:
begin
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
rescue LoadError
end
So that the absence of rspec doesn't stop your rakefile loading.
FWIW, I just faced the same issue. I fixed it by moving rspec-rails to production as shown below. I don't know why it works, but it works for me.
group :production, :development, :test do
gem 'rspec-rails', '~> 3.8'
end

Rake Default namespace runs tests

I'm attempting to run some tests on my rails application, and they're working, which is great. However, I'm noticing that when I just run rake it defaults to running my tests. If anybody has run into this before and can shed some light on why this is happening, I'd appreciate it.
I'm using
rails 4.1.0
ruby 2.0.0
factory girl rails
minitest rails
minitest rails capybara
database cleaner
Rakefile
require File.expand_path('../config/application', __FILE__)
Pinteresting::Application.load_tasks
namespace :test do
task :run do
ENV["RACK_ENV"] = "test"
$LOAD_PATH.unshift("lib", "spec")
if ARGV[1]
require_relative ARGV[1]
else
Dir.glob("./spec/**/*_spec.rb").each { |file| require file }
end
end
end
The default rake task is defined in rails/railties/Rakefile, and it runs all unit tests by default.

How to test JavaScripts without to delete the `test` database data?

I am using Ruby on Rails 3.2.2, cucumber-rails-1.3.0, rspec-rails-2.8.1 and capybara-1.1.2. I would like to use Selenium in order to test JavaScripts, but without to delete the test database data each time I run the cucumber command line in my Terminal window. That is, if I state a feature like the following:
Feature: ...
#javascript
Scenario: ...
JavaScript is tested as well as expected. However, after the test has run, the test database data is deleted and I must seed again that database in order to properly run new tests.
I read the Official Documentation and the text present in the ROOT_APP/features/support/env.rb file (it seems that I installed all required Ruby gems - see below for more information about the Gemfile that I am using) but I didn't understand how to avoid to delete the database data and how to configure Cucumber and Capybara gems so to properly work with Selenium.
What should I make?
Note I: I would like to make the above because I would like to have the same test database data when I "test"/"run" Scenarios.
Note II: In order to seed data in the test database (my application needs that data to work), I add the following code in the RAILS_ROOT_PATH/lib/tasks/cucumber.rake file and I run the rake db:test:prepare command line from the Terminal window.
namespace :db do
namespace :test do
task :prepare => :environment do
Rake::Task["db:seed"].invoke
end
end
end
In the ROOT_APP/features/support/env.rb file I tried to uncomment one and both of the following blocks of code (BTW: I never changed the original file auto-generated by the cucumber-rails gem, so it is the default one), but after running tests it still deletes the test database data.
# Before('#no-txn,#selenium,#culerity,#celerity,#javascript') do
# # { :except => [:widgets] } may not do what you expect here
# # as tCucumber::Rails::Database.javascript_strategy overrides
# # this setting.
# DatabaseCleaner.strategy = :truncation
# end
#
# Before('~#no-txn', '~#selenium', '~#culerity', '~#celerity', '~#javascript') do
# DatabaseCleaner.strategy = :transaction
# end
Gemfile excerpt:
group :development, :test do
gem "rspec-rails"
end
group :test do
gem 'cucumber-rails'
gem 'database_cleaner'
gem 'capybara'
end
I ran into this same problem, and managed to fix it by changing the following line in ROOT_APP/features/support/env.rb
from
Cucumber::Rails::Database.javascript_strategy = :truncation
to
Cucumber::Rails::Database.javascript_strategy = :transaction
Hope this helps...

How can I use FactoryBot in db/seeds?

Is it possible to do this?
If so, how can you do it?
Note: FactoryBot was previously named FactoryGirl
All you need to do is add require 'factory_bot_rails' to the db/seeds.rb file. This will give you access to your factories.
Note: Gem was previously called FactoryGirlRails
Josh Clayton, the maintainer of FactoryGirl, recommends against using FactoryGirl in your seeds file. He suggests using plain ActiveRecord instead.
(This answer works in rails 3.0.7)
I found the catch is how you set up the Gemfile - you need to do something along the lines of
gem 'factory_girl'
group :test do
gem 'factory_girl_rails'
end
We found problems having factory_girl_rails outside of the :test environment, which we didn't manage to get to the bottom of (maybe something to do with the way rails does class caching?)
Once that is done, I like to actually load data from a library in lib, something like...
require 'factory_girl'
require 'spec/factories/user_factory'
module Seeds
class SampleUsers
def self.run
u = Factory(:user)
end
end
And then running this method from within db:seed using
Seeds::SampleUsers.run
in db/seeds.rb
require 'factory_girl_rails'
10.times do
FactoryGirl.create :user
end
In Rails 5.2.6, you can create factories in your db/seeds.rb file. Add include FactoryBot::Syntax::Methods at the top of your seeds.rb file. Below that line, you can create your factories – i.e. user1 = create(:user).
# db/seeds.rb
include FactoryBot::Syntax::Methods
user1 = create(:user)
You can insert the following code into your spec_helper.rb, and it make some instances of the data you want (in this case "products" from the yaml file):
seeds_file = File.join(Rails.root, 'db', 'seeds.yml')
config = YAML::load_file(seeds_file)
config["products"].each do |product|
FactoryGirl.create(:product, product) if !Product.find_by_name(product['name'])
end

Resources