How to Stop RSpec Warning Messages - ruby-on-rails

I have just started learning to use RSpec on my rails application.
It all seems to work ok, but when I run rspec spec I get pages and pages of what I think are lint messages.
I don't mind the ones that refer to my code, but lots of them refer to Gems that I am using.
I can't really fix those.
How can I configure RSpec to apply lint only to my code and not external Gems?
Here is a small sample. I get over 2000 lines of this stuff.
/Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/gems/sorcery-0.8.5/lib/sorcery/model.rb:265: warning: method redefined; discarding old username_attribute_names=
/Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-3.2.18/lib/active_support/dependencies.rb:251: warning: loading in progress, circular require considered harmful - /Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/gems/sorcery-0.8.5/lib/sorcery.rb
from /Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/bin/ruby_noexec_wrapper:14:in `<main>'
from /Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/bin/ruby_noexec_wrapper:14:in `eval'
from /Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/bin/rspec:23:in `<main>'
from /Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/bin/rspec:23:in `load'
from /Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/gems/rspec-core-3.0.0/exe/rspec:4:in `<top (required)>'
from /Users/jcreasey
This is my .rspec file
-color
--warnings
--require spec_helper

If you have /.rspec file remove --warning option from it.

Related

Rails : RuntimeError - can't modify frozen Array when running rspec in rails

I recently upgraded to Rails 5.1 from v4.3 and am now getting this error when running tests:
An error occurred while loading
./spec/controllers/admin/capacity_charges_controller_spec.rb.
Failure/Error: require File.expand_path('../../config/environment', __FILE__)
RuntimeError:
can't modify frozen Array
I get it for every test file. The line that triggers the error comes from rails_helper. I've checked rails 5.1 sample repos and there's nothing substantially different about our version.
The full stack trace is:
RuntimeError:
can't modify frozen Array
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/actionpack-5.1.0/lib/action_dispatch/middleware/stack.rb:74:in `insert'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/actionpack-5.1.0/lib/action_dispatch/middleware/stack.rb:74:in `insert'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/actionpack-5.1.0/lib/action_dispatch/middleware/stack.rb:81:in `insert_after'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/request_store-1.3.2/lib/request_store/railtie.rb:5:in `block in <class:Railtie>'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/railties-5.1.0/lib/rails/initializable.rb:30:in `instance_exec'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/railties-5.1.0/lib/rails/initializable.rb:30:in `run'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/railties-5.1.0/lib/rails/initializable.rb:59:in `block in run_initializers'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/railties-5.1.0/lib/rails/initializable.rb:58:in `run_initializers'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/railties-5.1.0/lib/rails/application.rb:353:in `initialize!'
# ./config/environment.rb:5:in `<top (required)>'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:292:in `require'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:292:in `block in require'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:258:in `load_dependency'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:292:in `require'
# ./spec/rails_helper.rb:12:in `<top (required)>'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:292:in `require'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:292:in `block in require'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:258:in `load_dependency'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:292:in `require'
# ./spec/controllers/admin/capacity_charges_controller_spec.rb:3:in `<top (required)>'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:286:in `load'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:286:in `block in load'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:258:in `load_dependency'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:286:in `load'
Turns out this was caused by test failures higher in the stack trace. It was hard to see what the root cause was because there was so much in the console. If you get this issue, I'd suggest using --fail-fast so you don't get overwhelmed with recurrent error messages. Then check what the first error is.
For me it was a FactoryBot update from version 4 to 5. I didn't see the deprecation warning because I jumped versions:
DEPRECATION WARNING: Static attributes will be removed in FactoryBot 5.0.
Sometimes this exception is also raised if you have a wrong class name inheritance in your Ruby classes. Remember that controllers, models, etc, are all constants in Ruby.
This is an example off the top-of-my-head of a situation I encountered. Actual syntax may be off, but this was the concept that caused my problem.
module Admin
class BaseController < ::ApplicationController
...
end
end
# throws error, referencing constant that does not exist.
# Should be < ::Admin::BaseController
class OtherController < ::BaseController
end
Like others, my entire rspec suite would fail with the error can't modify a frozen Array, which isn't a very helpful stacktrace in this instance, per se.
Hope this save someone else from spending hours hunting down everything else!
Check that your Test DB is up to date before anything else.
rake db:test:prepare.
I'll post my cause, but first I'll post a troubleshooting path to help you find your cause, as this error can show up for a myriad of reasons.
This is a fairly useless error in most contexts, and the stack trace is impenetrable. I've exampined all the gem code in the stack, and none of it points to the actual source of the problem:
# /Users/dh/.gem/gems/actionpack-5.2.3/lib/action_dispatch/middleware/stack.rb:76:in `insert'
# /Users/dh/.gem/gems/actionpack-5.2.3/lib/action_dispatch/middleware/stack.rb:76:in `insert'
# /Users/dh/.gem/gems/actionpack-5.2.3/lib/action_dispatch/middleware/stack.rb:83:in `insert_after'
# /Users/dh/.gem/gems/request_store-1.4.1/lib/request_store/railtie.rb:5:in `block in <class:Railtie>'
# /Users/dh/.gem/gems/railties-5.2.3/lib/rails/initializable.rb:32:in `instance_exec'
# /Users/dh/.gem/gems/railties-5.2.3/lib/rails/initializable.rb:32:in `run'
# /Users/dh/.gem/gems/railties-5.2.3/lib/rails/initializable.rb:61:in `block in run_initializers'
# /Users/dh/.gem/gems/railties-5.2.3/lib/rails/initializable.rb:60:in `run_initializers'
# /Users/dh/.gem/gems/railties-5.2.3/lib/rails/application.rb:361:in `initialize!'
# ./config/environment.rb:5:in `<top (required)>'
To track down why you're getting this error, first comment out (or change back) whatever you did last and see if the problem goes away. If not, restore that line of code, then git stash to hide all your recent changes and run tests.
That will probably fix the issue, and you can git stash pop and start dividing your changes in half until you identify where the problem is.
If git stash does not fix the issue, then you know the problem is either external to your code, or was present in your last commit.
If you know your previous commit tested clean, start looking at your databases, bundle your gems, changes to ENV vars you use, or other environmental factors your app depends on.
If git stash did not fix the problem, and you don't know if your last commit was previously testing clean, start digging back through your commits, either manually or with git bisect. If you can find a commit that started breaking tests, you'll be able to track down the line that is triggering this error.
--
As for my particular issue, it was a typo in a new has_many relationship I added:
has_many :marijunas, class_name: Drug
Once I narrowed down the change to that line, the fix became obvious:
has_many :marijunas, class_name: :Drug
I did rspec | less to find the first error
When i got the same error with similar stacktrace, it helped me to fix a syntax error in factories.
In case this helps anyone in future, it was database connection issue (Postgresql in my case). Head over to config/database.yml file and supply username: and password: < default is toor>
Add this in the development: group.
Suggestion: try loading the app in development after adding config.eager_load = true to your development.rb file, which should give you an actually useful error message pinpointing the problem code.
In my case, a Rails 5.2 app, it was purely a result of upgrading from Ruby 2.4.5 to 2.5.8 which pinpointed the sole issue was unmatched range specifier in char-class in a very long regex that has been working for years.
I think when i had similar problem, it was due to syntax error. Try to run rails db:migrate to show more information on the line where error occur
In my case recent rails_admin required some addition
RuntimeError: Required middlewares for RailsAdmin are not added
To fix tihs, add
config.middleware.use ActionDispatch::Flash
to config/application.rb.
Then all rspecs became green again.
It was because I forgot to add bullet to the test group in Gemfile, looking at the top of the errors I saw:
NameError:
uninitialized constant Bullet
I had this same issue when working on an application with Rails 6.
The issue was that when I ran the command rspec I get the error below. The output below is shortened:
An error occurred while loading ./spec/routing/applyportal/school_histories_routing_spec.rb.
Failure/Error: require File.expand_path('../config/environment', __dir__)
FrozenError:
can't modify frozen Array: ["/home/promisechukwuenyem/Projects/myapp/app/channels",
Here's how I solved it:
The issue was a result of errors in my code after I did a git pull.
Since I was running in development, I tried dropping my database using:
rails db:drop
Note: This did not drop my database, as the command was interrupted by the error in my code.
And then the actual place where the error was showed up in my terminal which was my config/route.rb file.
I had open my config/route.rb file. And then I found this:
# frozen_string_literal: true
Rails.application.routes.draw do
<<<<<<< HEAD
resources :courses
=======
resources :results
>>>>>>> Added result and result upload feature
resources :books
resources :schools
end
And then modified it to this:
# frozen_string_literal: true
Rails.application.routes.draw do
resources :books
resources :results
resources :schools
end
That's all.
I hope this helps
In my case, it turned out to be an issue with a config.after_initialize block failure, hidden during a rspec run.
However, the error surfaced when I called $ rails c -e test. Hope this helps someone with this being my first post.
I tried to update from Rails 3 to Rails 5.
I just set config.eager_load option and it helped. Either to false or true. Not nil, please
In my case it was due to a git conflict message (the one like >>>> ... === ... <<<<) that I missed to resolve, so there was a syntax error when Rspec has been trying to load that class.
--fail-fast command line option does not help in this case as the error happens higher in the call stack, but you can see the actual error by just running any single spec file.
My two-cents, as it may help someone else. I had this same issue when upgrading rails 4.2 to 5.0 I saw a thread post somewhere that suggested running one spec-test by itself / at a time. When I did that, I got a different error: superclass must be a Class and pointed me to my new .../models/application_record.rb file, in which I had made a syntax error by omission:
class ApplicationRecord < ActiveRecord
self.abstract_class = true
end
The above is missing ::Base after ActiveRecord -- first line.
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
Adding that back in fixed all my specs.
Try setting config.eager_load = true in your environment file. This will load all files in memory during boot & will point out existing syntax errors in first place if any.
I got this error when I removed the gem 'Faker' but left the require 'faker' inside factories.rb
Try to run redis and sidekiq on background. In my case it was one of reasons.

Ruby 'requires' are throwing NoMethodError's, stack trace shows Unix paths on a Windows machine

Trying to run a ruby script, but it's failing on the 'require' statements, throwing a strange error:
Code:
require "rubygems"
require "bundler/setup"
require_relative "plugins/pivotal_rabbitmq_plugin/pivotal_rabbitmq_plugin.rb"
require "newrelic_plugin"
module PivotalAgent
NewRelic::Plugin::Config.config_file=File.dirname(__FILE__) + "/config/newrelic_plugin.yml"
NewRelic::Plugin::Run.setup_and_run
end
Command:
ruby pivotal_agent.rb
Error:
/usr/share/rubygems/rubygems/path_support.rb:78:in `path=': undefined method `+' for nil:NilClass (NoMethodError)
from /usr/share/rubygems/rubygems/path_support.rb:34:in `initialize'
from /usr/share/rubygems/rubygems.rb:325:in `new'
from /usr/share/rubygems/rubygems.rb:325:in `paths'
from /usr/share/rubygems/rubygems.rb:348:in `path'
from /usr/share/rubygems/rubygems/specification.rb:872:in `dirs'
from /usr/share/rubygems/rubygems/specification.rb:750:in `stubs'
from /usr/share/rubygems/rubygems/specification.rb:938:in `find_inactive_by_path'
from /usr/share/rubygems/rubygems.rb:186:in `try_activate'
from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:126:in `rescue in require'
from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:39:in `require'
from pivotal_agent.rb:2:in `<main>'
Any ideas what might be causing that? My initial hunch is that it has something to do with the file paths it's looking at being formatted for Unix while this is running on a Windows machine, but I've added Ruby to PATH, so I don't know why it would be looking there rather than where Ruby is actually installed (C:\Ruby22-x64).
EDIT: To clarify, the first require (for 'rubygems') doesn't throw an error, but both the other requires and the require_relative all throw a similar one.
Not 100% sure why, but removing the 'ruby' from the start of the command fixed it. So just running
my_script.rb
works fine, but running
ruby my_script.rb
gives the above stack.

I cant run my rspec in Ruby on Rails

I cant really get going with my Rspec-gem :) Unfortunately. It seems like the Rspec-rails gem works differently now? Any idea?
This is my error Im getting:
testing_rspec/spec/models/transport_spec.rb:2:in `<top (required)>': uninitialized constant Transport (NameError)
from /Users/yoniPacheko/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rspec-core-3.0.3/lib/rspec/core/configuration.rb:1057:in `load'
My spec file:
require 'spec_helper'
describe Transport do
end
This is my file
Thanks for yr time!
If this is a newly-generated app, you should be using require 'rails_helper', instead of requiring spec_helper.
rspec-rails 3 has split the two out so that spec_helper can be used for standalone Ruby code, and rails_helper (which includes spec_helper) can be used for testing code that relies on Rails.

Rails 3 - How do I get Rspec spec/ & autotest to work?

I was all setup with Rspec and Autotest before I updated to Rails 3.
When running rspec spec/ I'm getting:
* spec/spec.opts is deprecated.
* please use ./.rspec or ~/.rspec instead.
I tried changed spec.opts to spec.rspec and running rspec spec/ again. But now I get a whole mess of warnings:
DEPRECATION WARNING: Use toplevel init.rb; rails/init.rb is deprecated:
DEPRECATION WARNING: reset_javascript_include_default is deprecated. Please manipulate config.action_view.javascript_expansions[:defaults] directly.
DEPRECATION WARNING: RAILS_ENV is deprecated. Please use ::Rails.env.
DEPRECATION WARNING: Use toplevel init.rb; rails/init.rb is deprecated:
DEPRECATION WARNING: You are using the old router DSL which will be removed in Rails 3.1. Please check how to update your routes file at: http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/.
Autotest isn't working either. I'm getting:
.rvm/rubies/ruby-1.9.2-p0/bin/ruby -I.:lib:test -rubygems -e "['test/unit', 'spec/integration/layout_links_spec.rb', 'test/unit/color_test.rb', 'test/functional/products_controller_test.rb', 'spec/integration/users_spec.rb', 'test/functional/colors_controller_test.rb', 'test/unit/product_test.rb', 'test/unit/helpers/products_helper_test.rb', 'test/unit/helpers/colors_helper_test.rb'].each { |f| require f }" | .rvm/gems/ruby-1.9.2-p0/gems/autotest-4.3.2/bin/unit_diff -u
:29:in require': no such file to load -- spec_helper (LoadError)
from <internal:lib/rubygems/custom_require>:29:inrequire'
from spec/integration/layout_links_spec.rb:1:in <top (required)>'
from <internal:lib/rubygems/custom_require>:29:inrequire'
from :29:in require'
from -e:1:inblock in '
from -e:1:in each'
from -e:1:in'
Any ideas?
The deprecation warnings are because of your upgrade to Rails 3, not because of RSpec. Rails 3 has a very different architecture, and the API has also changed significantly. Have you run your project through the rails_upgrade plugin? Also, check out the Rails 3 Release Notes for help on fixing the parts of your application that rely on deprecated functionality.
The second issue is probably because ruby 1.9.2 no longer puts the current directory on the load path. You'll need to manually include spec/ into the load path from your spec.opts.

rake spec not using the rails environment

I'm attempting to use rspec in a rails project I've just upgraded to rails 2.3.2. I've installed rspec 1.2.6 and rspec-rails 1.2.6 as plugins in the app.
My problem is the specs don't have access to my app classes or any of the rails standard libraries.
First I had to specify the model class I want to test by using the full path from RAILS_ROOT but now as it loads the class I get the following
/app/models/person.rb:1: uninitialized constant ActiveRecord (NameError)
from ./spec/models/person_spec.rb:1:in `require'
from ./spec/models/person_spec.rb:1
from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:15:in `load'
from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:15:in `load_files'
from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:14:in `each'
from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:14:in `load_files'
from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/options.rb:99:in `run_examples'
from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/command_line.rb:9:in `run'
from /Users/law/Projects/roster/vendor/plugins/rspec/bin/spec:4
rake aborted!
I am launching rspec by calling rake spec from the root of the application.
Any ideas on what might be missing in this situation?
you need indeed include the spec_helper.rb in every spec file you write....
You can run individual specs that way:
$ spec specs/models/person_spec.rb
instead of always running the whole spec suite
I havn't used spec, so this may not solve your problem, but if you're writing your own rake task and need your rails environment, you have to ask for it.
task(:task_name => :environment) do
# Task Implementation Here
end

Resources