1 year ago

#284276

test-img

Christopher Marchant

Ruby on Rails 6 with Minitest: Is there a way to shorten the outputted error?

I'm going through Section 3.3 of The Ruby on Rails Tutorial by Michael Hartl and we just started the section on testing. In this section, we're grouping static pages in a controller (StaticPagesController) and are trying manually to add an "about" action to the controller. Because we are practicing TDD, we are writing a failing test first. However, the outputted error message is extremely long (I don't know if this is standard), and I want to know if there is a way to shorten the output. Thanks in advance!

test/controllers/static_pages_controller_test.rb: we write the following code (two previous and passing tests omitted for brevity):

require "test_helper"

class StaticPagesControllerTest < ActionDispatch::IntegrationTest
  test "should get about" do
    get static_pages_about_url
    assert_response :success
  end
end

I then run the following command in the terminal to run the tests:

% rails test

As expected, the output to the terminal shows 1 error. However, the error is extremely long (so long that it does not fit in the VS Code terminal -- I have to view it in the system terminal) and I have to scroll up hundreds of lines to see the start of the offending error.

Sample of Error:

Minitest::UnexpectedError: NameError: undefined local variable or method `static_pages_about_url' for #<StaticPagesControllerTest:0x00007fefbd442320 @_routes=nil, @NAME="test_should_get_about", @failures=[#<Minitest::UnexpectedError: Unexpected exception>], @assertions=0, @integration_session=#<#<Class:0x00007fefbd441c40>:0x00007fefbd4414c0 @_routes=nil, @app=#<SampleApp::Application:0x00007fefbd2fc308 @_all_autoload_paths=["/Users/christophermarchant/Documents/odin-project/full-stack-ruby/rails-course/hartl_rails_tutorial/sample_app/environment/sample_app/app/channels", "/Users/christophermarchant/Documents/odin-project/full-stack-ruby/rails-course/hartl_rails_tutorial/sample_app/environment/sample_app/app/controllers", ... @stubs=#<Concurrent::Map:0x00007fefbcd08550 entries=0 default_proc=#<Proc:0x00007fefbcd084b0 /Users/christophermarchant/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/activesupport-6.1.5/lib/active_support/testing/time_helpers.rb:14>>>, @time=0.12419799994677305>

The entire error is much longer.

It appears that Rails is inspecting and printing the entire StaticPagesControllerTest object with all of its attributes.

MAIN QUESTION: Is there a way to change some configuration so the NameError is shorter, like the following?

NameError: undefined local variable or method `static_pages_about_url' for #<StaticPagesControllerTest:0x00007fefbd442320>

OTHER INFO

Gemfile:

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '3.0.3'
gem 'rails', '~> 6.1.5'
gem 'puma', '~> 5.0'
gem 'sass-rails', '>= 6'
gem 'webpacker', '~> 5.0'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.7'

gem 'bootsnap', '>= 1.4.4', require: false

group :development, :test do
  gem 'sqlite3', '~> 1.4'
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  gem 'web-console', '>= 4.1.0'
  gem 'rack-mini-profiler', '~> 2.0'
  gem 'listen', '~> 3.3'
  gem 'spring'
end

group :test do
  gem 'capybara', '>= 3.26'
  gem 'selenium-webdriver', '>= 4.0.0.rc1'
  gem 'webdrivers', '~> 5.0'
  gem 'rails-controller-testing', '~> 1.0', '>= 1.0.5'
  gem 'minitest', '~> 5.15'
  gem 'minitest-reporters', '~> 1.5'
  gem 'guard', '~> 2.18'
  gem 'guard-minitest', '~> 2.4', '>= 2.4.6'
end

group :production do
  gem 'pg', '~> 1.3', '>= 1.3.4'
end

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

test/test_helper.rb file:

ENV['RAILS_ENV'] ||= 'test'
require_relative "../config/environment"
require "rails/test_help"
require "minitest/reporters"

class ActiveSupport::TestCase
  # Run tests in parallel with specified workers
  parallelize(workers: :number_of_processors)

  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

  # Add more helper methods to be used by all tests here...
end

Minitest::Reporters.use!(
  Minitest::Reporters::SpecReporter.new(:color => true),
  ENV,
  Minitest.backtrace_filter
)

System and Versions:

Ruby on Rails 6.1.5

Ruby 3.0.3p157

macOS Big Sur Version 11.6.4

ruby-on-rails

ruby

ruby-on-rails-6

minitest

0 Answers

Your Answer

Accepted video resources