If you are a Ruby on Rails developer, testing is your bread, it's in your blood. Or maybe you haven't had to deal with the tests yet, because you are just starting the adventure with the DHH child. Anyway, I have a tip for you that will make your testing much more readable and convenient. And every time you use the RSpec password, you will think "readable test documentation".
For testing, I use the most popular library in the RoR world - RSpec. Additionally, my setup complements DatabaseCleaner, ShouldaMatchers, and FactoryBotRails.
group :test do
gem 'rspec-rails',
gem 'factory_bot_rails'
gem 'shoulda-matchers'
gem 'database_cleaner'
end
ENV["RAILS_ENV"] ||= "test"
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'shoulda/matchers'
Dir[Rails.root.join("spec/support/**/*.rb)].each { |f| require f }
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = example.metadata[:js] ? :truncation : :transaction
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
The code we will test looks like this:
class Fotballer < ApplicationRecord
validates :name, presence: true, length: { maximum: 250 }
validates :surname, presence: true, length: { maximum: 250 }
validates :club, length: { maximum: 250 }
validates :nationality, presence: true, length: { maximum: 30 }
end
require 'rails_helper'
RSpec.describe Footballer, type: :model do
describe "database columns" do
it { should have_db_column(:name).of_type(:string) }
it { should have_db_column(:surname).of_type(:string) }
it { should have_db_column(:club).of_type(:string) }
it { should have_db_column(:nationality).of_type(:string)
end
end
With the basic configuration of RSpec, placed in the spec_helper.rb
file and performing tests, the console does not print too readable messages, and it is impossible to track the progress of individual tests. Let's just look at what we get.
What if we would like to monitor the test progress on an ongoing basis and check what has already been done and is green, and what has not passed? Just slightly change RSpec configuration in spec_helper.rb
and set config.color
to true
and config.formatter
to :documentation
.
RSpec.configure do |config|
config.color = true
config.tty = true
config.formatter = :documentation
end
The test result looks much better than before.
Personally, I attach great importance to how the test is printed on the console. In the event of an error, it's much easier to find out where the problem lies. Besides, by checking the entire system, I am able to estimate how many more corrections are waiting for me while the tests are running.
However, if you want to get off the console and editor and use the only valid RoR IDE at the moment, which is Ruby Mine, the tests are well served there. I prefer this tool to. One click is enough and the test progress is clear and understandable. It is important to choose something according to your own preferences.
Anyway, for me, the statement: RSpec - clear test documentation will always be the same.
Read about it in the RSpec documentation.
Top comments (0)