Rails i18n spec doesn't work - ruby-on-rails

In my rails app, I use the gem i18n_spec to add rspec tests that check if my i18n config files are correct.
The spec file
require 'spec_helper.rb'
require 'i18n-spec/tasks'
Dir.glob('config/locales/*.yml') do |locale_file|
describe "#{locale_file}" do
it { subject.should be_parseable }
results in errors
../gems/i18n-spec-0.6.0/lib/i18n-spec/tasks.rb:5:in `<top (required)>': undefined method `namespace' for main:Object (NoMethodError)
from ../gems/backports-3.3.3/lib/backports/tools.rb:328:in `require'
from ../gems/backports-3.3.3/lib/backports/tools.rb:328:in `require_with_backports'
from ../gems/activesupport-3.2.21/lib/active_support/dependencies.rb:251:in `block in require'
from ../gems/activesupport-3.2.21/lib/active_support/dependencies.rb:236:in `load_dependency'
from ../gems/activesupport-3.2.21/lib/active_support/dependencies.rb:251:in `require'
from ../spec/selftest/i18n_spec.rb:3:in `<top (required)>'

Why do you want to test if they are parseable? If you are asserting any I18n key in a unit / feature spec Rails will load your I18n files and throw a syntax error if the yml files aren't parseable!
You will get assertions like so:
within(".mod-header .login") { click_link I18n.t('public.menu.login') }
find("#main-container h1").should have_content I18n.t('pages.login.title')
This way Rails will load and parse your yml files. And if there are any errors you will find them in your test output!
And errors like this:
<Psych::SyntaxError: (your_project_path/config/locales/nl.yml): mapping values are not allowed in this context at line <num> column <num>>
or
<Psych::SyntaxError: (your_project_path/config/locales/nl.yml): did not find expected key while parsing a block mapping at <num> column <num>>

Related

How can I fix the "class variable ##debug_missing_translation of ActionView::Base is overtaken by Object" error when running any task with ruby 3.0?

I am trying to upgrade a Rails 6.1.3.1 application from Ruby 2.6.6 to 3.0.0
All the rspec tests run fine and the in development everything seems to work just fine except one thing:
Even the simplest of tasks fails with this error:
"class variable ##debug_missing_translation of ActionView::Base is overtaken by Object"
For example, this simpletask.rake file
task simpletask: :environment do
puts 'Hello'
end
is not able to run as I get the following error:
lxxx#xxx:~/Workspace/edumino$ rails simpletask
rails aborted!
class variable ##debug_missing_translation of ActionView::Base is overtaken by Object
/home/xxx/railsapp/config/environment.rb:5:in `<top (required)>'
/home/xxx/railsapp/bin/rails:5:in `require'
/home/xxx/railsapp/bin/rails:5:in `<top (required)>'
/home/xxx/railsapp/bin/spring:10:in `require'
/home/xxx/railsapp/bin/spring:10:in `block in <top (required)>'
/home/xxx/railsapp/bin/spring:7:in `<top (required)>'
Tasks: TOP => simpletask => environment
(See full trace by running task with --trace)
...so, it turns out the problem was an already existing task. I don't remember exactly why this was necessary but the task had this:
# frozen_string_literal: true
require "#{Rails.root}/app/helpers/cron_helper"
require "#{Rails.root}/app/helpers/notifier_helper"
require "#{Rails.root}/app/helpers/application_helper"
include ApplicationHelper
include ActionView::Helpers::TranslationHelper
include CronHelper
namespace :cron do
task sms: :environment do |_t, _args|
...
end
end
I assume that the guilty code was including the ActionView::Helpers::TranslationHelper
Again, I can't remember why I had to do that at that time. But if you bump into this kind of 'overtaken by' error when upgrading to Ruby 3.0, check your other tasks and see what kinda weird includes you might have had. In my case, since I didn't need the old task, I just removed it.

Failure/Error: config.include ::Rails::Controller::Testing::TemplateAssertions, type: :controller

I am trying to run a single test file: bundle exec rspec spec/models/user_spec.rb
But get following error:
An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error: config.include ::Rails::Controller::Testing::TemplateAssertions, type: :controller
NameError:
uninitialized constant Rails::Controller
Did you mean? ApiController
# ./spec/rails_helper.rb:149:in `block in <top (required)>'
# ./spec/rails_helper.rb:61:in `<top (required)>'
# ./spec/models/user_spec.rb:3:in `require'
# ./spec/models/user_spec.rb:3:in `<top (required)>'
No examples found.
Initially, I was getting following error:
An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error: module Shoulda::Matchers::ActiveModel
NameError:
uninitialized constant Shoulda
# ./spec/support/matchers/validate_kept_of_matcher.rb:4:in `<top (required)>'
# ./spec/rails_helper.rb:51:in `block in <top (required)>'
# ./spec/rails_helper.rb:51:in `each'
# ./spec/rails_helper.rb:51:in `<top (required)>'
# ./spec/models/user_spec.rb:3:in `require'
# ./spec/models/user_spec.rb:3:in `<top (required)>'
No examples found.
But then it fixed after I added following to the test file.
require "shoulda/matchers"
I am newbie in Ruby/Rails world, can someone please give a direction?
Rails::Controller::Testing::TemplateAssertions was removed in Rails 5.
You can re-add the depreciated functionality by installing the Rails controller testing gem. However the use of controller specs, assigns and template assertions is discouraged by both the RSpec and Rails teams and is not very future proof.
The community accepted solution is to write request specs and stop poking inside your controllers.

When I run the test “ rspec spec/cart_spec.rb ”In the console, I'll get a warning

When I run the test “ rspec spec/cart_spec.rb ”In the console, I'll get a warning
require 'rspec'
require_relative '../app/item'
require_relative '../app/virtual_item'
require_relative '../app/antique_item'
require_relative '../app/item_container'
require_relative '../app/cart'
describe Cart do
it 'add items into the cart' do
cart = Cart.new('')
item1 = Item.new('kettle', price: 200)
item2 = Item.new('car', price: 200)
cart.add_items(item1, item2)
cart.items.should include(item1, item2)
end
end
In the console, I'll get a warning
E:\work\storeapp\spec>rspec spec/cart_spec.rb
c:/tools/rubies/ruby-2.1.5-p273/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1226:in `load': cannot load such file -- E:/work/storeapp/spec/spec/cart_s
pec.rb (LoadError)
from c:/tools/rubies/ruby-2.1.5-p273/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1226:in `block in load_spec_files'
from c:/tools/rubies/ruby-2.1.5-p273/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1224:in `each'
from c:/tools/rubies/ruby-2.1.5-p273/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1224:in `load_spec_files'
from c:/tools/rubies/ruby-2.1.5-p273/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:97:in `setup'
from c:/tools/rubies/ruby-2.1.5-p273/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:85:in `run'
from c:/tools/rubies/ruby-2.1.5-p273/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:70:in `run'
from c:/tools/rubies/ruby-2.1.5-p273/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:38:in `invoke'
from c:/tools/rubies/ruby-2.1.5-p273/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/exe/rspec:4:in `<top (required)>'
from c:/tools/rubies/ruby-2.1.5-p273/bin/rspec:23:in `load'
from c:/tools/rubies/ruby-2.1.5-p273/bin/rspec:23:in `<main>'
![enter image description here][1]
where is my mistake?
The error says that it can't find the spec file and indeed you're pointing to
spec/cart_spec.rb
... while in the spec directory. To find it from there you need to remove spec/:
rspec cart_spec.rb
My usual practice is to run specs from the app root. From there, your original command should work. Or you can just use an absolute path instead of a relative path.

RSpec verbose error messages

When I run any RSpec test that fails, I get many lines of messages that I don't really understand.
For instance, suppose I run:
expect(true).to be_false
Then my console gets cluttered with messages beginning with #
Failures:
1) Some test
Failure/Error: expect(true).to be_false
expected true to respond to `false?`
# ./spec/controllers/wing_relationships_controller_spec.rb:43:in `block (3 levels) in <top (required)>'
# /Users/mac/.rvm/gems/ruby-2.1.2#global/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `load'
# /Users/mac/.rvm/gems/ruby-2.1.2#global/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `block in load'
# /Users/mac/.rvm/gems/ruby-2.1.2#global/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:232:in `load_dependency'
# /Users/mac/.rvm/gems/ruby-2.1.2#global/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `load'
# /Users/mac/.rvm/gems/ruby-2.1.2#global/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `load'
# /Users/mac/.rvm/gems/ruby-2.1.2#global/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `block in load'
# /Users/mac/.rvm/gems/ruby-2.1.2#global/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:232:in `load_dependency'
# /Users/mac/.rvm/gems/ruby-2.1.2#global/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `load'
# -e:1:in `<main>'
The error messages always vary, and sometimes they are extremely long.
When I run many tests, it gets hard to read the results. I would like to get rid of those. Any suggestions? I already turned off --warning in RSpec
It's better find out where the errors come from and fix the leaky specs rather the ignoring the errors.
If you are not interested in doing it for some reason then you can always use:
You use a simple trick if you really want to:
rspec 2>/dev/null
Got this answer from https://github.com/rspec/rspec-rails/issues/1237
To remove the gems from the output, it's one line of config:
RSpec.configure do |config|
config.backtrace_exclusion_patterns << %r{/gems/}
end
Or, if you want to only filter out particular gems from your backtraces:
RSpec.configure do |config|
config.filter_gems_from_backtrace "rack", "rack-test", "capybara"
end

uninitialized constant Test::Unit::UI::XML (NameError) in Rails (or Ruby)

I'm trying to create an xml runner to make a result report of Rails unit testing. Here is a code I have:
require 'test/unit'
require 'test/unit/ui/console/testrunner'
class FastFailRunner < Test::Unit::UI::Console::TestRunner
def add_fault(fault)
#faults << fault
nl
output("%3d) %s" % [#faults.length, fault.long_display])
output("--")
#already_outputted = true
end
def finished(elapsed_time)
nl
output("Finished in #{elapsed_time} seconds.")
nl
output(#result)
end
end
Test::Unit::AutoRunner::RUNNERS[:fastfail] = proc do |r|
FastFailRunner
end
When I run it as TESTOPTS="/home/alex/RubymineProjects/app2/test/unit/runner.rb --runner=xml" rake test
... I get an error (pretty weird error)
/home/alex/.rvm/gems/ruby-1.9.3-p194#global/gems/rake-0.9.2.2/lib/rake/ext/module.rb:36:in `const_missing': uninitialized constant Test::Unit::UI::XML (NameError)
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/runner/xml.rb:5:in `block in <module:Unit>'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:389:in `[]'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:389:in `run'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:58:in `run'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit.rb:501:in `block in <top (required)>'
gem install minitest
/home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/runner/xml.rb:5:in `block in <module:Unit>': uninitialized constant Test::Unit::UI::XML (NameError)
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:389:in `[]'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:389:in `run'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:58:in `run'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit.rb:501:in `block in <top (required)>'
Errors running test:units! #<RuntimeError: Command failed with status (1): [/home/alex/.rvm/rubies/ruby-1.9.3-p194/bin...]>
Errors running test:functionals! #<RuntimeError: Command failed with status (1): [/home/alex/.rvm/rubies/ruby-1.9.3-p194/bin...]>
I tried to require test/unit/ui/xml/testrunner.rb but no luck.
Any thoughts?
If you are in Ruby 1.8.7 you can say:
require 'test/unit/ui/console/testrunner'
p Test::Unit::UI::Console::TestRunner # => no problem
(There is no such file as test/unit/ui/xml/testrunner.rb so I'm not sure what you were up to there.)
Look in the docs in test/unit.rb, there's actually sample code showing you how to do this require: http://www.ruby-doc.org/stdlib-1.8.7/libdoc/test/unit/rdoc/Test/Unit.html.
However, the problem is that you are in Ruby 1.9.3. There is no /test/unit in Ruby 1.9.3! Well, there is, but it's just a compatibility layer for basic tests; there is certainly no test/unit/ui/console/testrunner, and no module/class Test::Unit::UI::Console::TestRunner.
Instead, there's minitest. You can read the docs on minitest to see how to make a test runner. http://docs.seattlerb.org/minitest/
One thing to consider is that Test::Unit was included in the default Ruby 1.8.7 installation. If you want to use it in with a later version of Ruby then go ahead and install it as a gem.
sudo gem install test-unit
http://test-unit.rubyforge.org/

Resources