Rails Rspec warning: "This dynamic method is deprecated." - ruby-on-rails

My Rails 4/Ruby 2 app is throwing the following warning every time my RSpec tests create a FactoryGirl object: "DEPRECATION WARNING: This dynamic method is deprecated. Please use e.g. Post.find_or_create_by(name: 'foo') instead."
This warning is not thrown when I run my app in development. Is FactoryGirl's code throwing this? I tried to find some information but it doesn't look like other people are getting this.

If you tell Rails to give you a full stack trace for the deprecation warning, you should be able to diagnose it pretty easily. The warnings are coming from a library called ActiveSupport::Deprecation - tell it to run in debug mode.
# config/environments/test.rb
ActiveSupport::Deprecation.debug = true
For me, the warnings were caused by an old version of the Stringex library.
FactoryGirl would make a new model, which would trigger a call to one of the Stringex methods, which would raise the warning, although there was no way to see that until I turned on full stack traces. bundle update stringex solved the issue with no problem.

It looks like it's coming from ActiveRecord.
module DeprecationWarning
def body
"#{deprecation_warning}\n#{super}"
end
def deprecation_warning
%{ActiveSupport::Deprecation.warn("This dynamic method is deprecated. Please use e.g. #{deprecation_alternative} instead.")}
end
end
I'm not sure why you're not getting the warnings in development. Is your environment suppressing the warnings?

Related

Rails undefined local variable or method `first' for ActiveRecord::NullMutationTracker:Class on local

We are getting this error on all local setups of our project(currently) when we call any method like:
belongs_to :abc
after_create :some_method
def some_method
if self.abc.saved_change_to_parent_id?
...
// or even self.abc.parent_id_before_last_save
end
It gives:
NameError (undefined local variable or method `first' for ActiveRecord::NullMutationTracker:Class):
app/models/model_name.rb:50:in `some_method'
Yes, there is not a full trace with rails internal file paths etc, i only get my project files trace. Maybe its some logger config issue, any help to get full trace will also be appreciated.
There are no such issues i could find on internet, thats why posting here.
PS: Not posted on rails issue tracker(github issues) because i don't have minimal reproduction.
Stack
ruby: 2.4.3
rails: 5.1.5 (also tried on 5.1.7)
OS: Ubuntu 20, also tried on macOS
Thanks in advance.
UPDATE1: Using byebug, i got to this trace, where error is occuring:
/Users/dev/.rvm/gems/ruby-2.4.3/gems/acts_as_singleton-0.0.8/lib/acts_as_singleton.rb:43
which is here, so it is not directly from rails, but a very outdated gem we have in our code for some reason.
As i posted in the update, issue was not with rails or anything, i used byebug to track trace and it was something like:
activemodel-5.1.5/lib/active_model/attribute_methods.rb:384
activerecord-5.1.5/lib/active_record/attribute_methods/dirty.rb:146
activerecord-5.1.5/lib/active_record/attribute_methods/dirty.rb:319
activerecord-5.1.5/lib/active_record/attribute_methods/dirty.rb:315
acts_as_singleton-859f49112c03/lib/acts_as_singleton.rb:43
The last line, should not be there, as we are not using acts_as_singleton on any of the involved models.
So after some tracking, it turned out issue with the gem. I have created a fork and used that fork. Here is the fork: https://github.com/ziaulrehman40/acts_as_singleton (forked it from another fork, which seemingly had some other fixes as well). And you can see my changes here.
What was the issue?
This gem writes a module named Singleton within ActiveRecord module. Which seems ok, unless you realize that there is another module named Singleton already. Whihc is being included in:
gems/activerecord-5.1.5/lib/active_record/attribute_mutation_tracker.rb:83
class NullMutationTracker # :nodoc:
include Singleton
...
So as you can see, this Singleton module gets overriden(or expanded, not sure) un-intentionally by that outdated gem(acts_as_singleton).

Treat warnings as failures

I'm trying to clean up my test suite and to make this easier I'd like to have rspec raise an error when ever it encounters a warning. Rather then pass the test and carry on, I'd like the test to fail. Is there a way I can configure rspec to do this?
Are you talking about deprecation warnings? Or warnings in general?
I know you can raise errors when you hit deprecation warnings by setting config.active_support.deprecation = :raise in your test.rb
Per the RubyDoc for RSpec on Method: RSpec::Matchers#output, you should be able to fail the test by using expect(method).to_not output.to_stdout. I'm no expert in using RSpec, but in theory that should work. RSpec-Puppet has a very similar functionality built in that fails on warnings by using Puppet.expects(:warning).never .
I'm not sure this will work everywhere, since there is no contract that warnings have to be issued through Logger#warn, but this might do a decent job.
class Logger
def warn(*args)
super
if Rails.env.test?
raise(RuntimeError, "failing upon warn")
end
end
end
log = Logger.new
log.warn("foo")
Rspec gives us a simple way to do this globally:
config.before do
expect_any_instance_of(Logger).to_not receive(:warn)
end

factory_girl linter failing because of ActionView::Template::Error

I'm in the process of working out the bugs for updating an app from rails 4.1 to rails 5. When I try to run my tests the factory_girl linter complains that some of the factories are invalid:
/Users/stephen/.rvm/gems/ruby-2.3.1#ssa/gems/factory_girl-4.7.0/lib/factory_girl/linter.rb:13:in `lint!': The following factories are invalid: (FactoryGirl::InvalidFactoryError)
* call_log - Undefined variable: "$green". (ActionView::Template::Error)
* sms_log - Undefined variable: "$green". (ActionView::Template::Error)
etc.
This is all factory girl really gives me for a stack trace. It tells me the where the linter is called but that is it. No indication where something might be going in the view. Is there a way for me to determine this? It seems like this is going to be very difficult to debug fi I don't know where it's coming from.
Having investigated by sitting right next to you, I am going to go out on a limb and say that the model has a callback that sends an email, which triggers the whole ActionView rendering, which apparently has some errors.
More proof that callbacks are bad - particularly ones that send emails.

SslRequirement.disable_ssl_check error undefined method?

I am working on an established project, Rails 3, and trying to turn of SSL in development. The following line in environments/development.rb generates an error.
config.after_initialize do
SslRequirement.disable_ssl_check = true
end
Error reads:
undefined method `disable_ssl_check=' for SslRequirement:Module (NoMethodError)
Any pointers to this?
Searches have revealed little so far.
Thank you
Based on the version number you provided (0.1.0) the problem is likely caused by the fact that you are using a different gem.
The one you use is ssl_requirement (repo), whereas the disable_ssl_check method is provided by bartt-ssl_requirement (repo). The latter one is actually a fork of the former one so you might try to upgrade.

What does "Anonymous modules have no name to be referenced by" really mean?

I'm upgrading my Rails app to work with Ruby 1.9 and I keep encountering errors like this:
Anonymous modules have no name to be referenced by
/home/foo/.gem/ruby/1.9.1/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:585:in `to_constant_name'
/home/foo/.gem/ruby/1.9.1/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:391:in `qualified_name_for'
/home/foo/.gem/ruby/1.9.1/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:104:in `rescue in const_missing'
/home/foo/.gem/ruby/1.9.1/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:94:in `const_missing'
/home/foo/app/config/environment.rb:66:in `block in <top (required)>'
etc.
Google finds all kinds of hits for this, but each of them pertains to a specific fix for one specific gem or app. None of them explain what the message really means.
What is an "anonymous module"?
Where is this error message coming from? (The Ruby interpreter itself?)
What is different about Ruby 1.9 that causes this? (Rails 2.3.8 with Ruby 1.8.7 does not encounter this.)
What is the general/proper way to fix this error?
Line 66 of environment.rb is the configuration for super_exception_notifier (old version, 2.0.8):
ExceptionNotifier.configure_exception_notifier do |config|
config[:sender_address] = %("Foo" <foo#foo.com>)
config[:exception_recipients] = %w(foo#foo.com)
config[:skip_local_notification] = false
end
From what I can tell, ExceptionNotifier is undefined, and ActiveSupport is trying to magically load it, but fails and then fails again trying to print a nice error message.
An anonymous module is a module that is declared like so:
Fred = Module.new do
def meth1
"hello"
end
def meth2
"bye"
end
end
instead of by using the regular Module mod_name <block> syntax. Since they have no module name, you can't retrieve the module name. to_constant_name is attempting to call desc.name.blank? where desc is an anonymous module (with no name).
This error is coming from the ActiveSupport module, which may indicate a bug in the active_support gem or may indicate that some other piece of code is using ActiveSupport incorrectly. The error message alone doesn't give enough information to identify the culprit (to me at least, someone with more rails experience might be able to provide more insight).
Without knowing the offending code it's also hard to say exactly why this error is popping up with 1.9, or what needs to be done to fix it. Considering that there are a lot of un- and under-maintained gems out there that have not been updated for 1.9 yet, I would suspect that ActiveSupport is not the source of the problem. Upgrade all of your gems that have 1.9-compatible versions, and then try disabling your other gems one at a time (if you can) and see if you still get the error.
If you provide a list of the other gems that you are using, someone else who may have encountered the error before may be able to provide some details.
This may happen if you try to exploit ActiveRecord's internal class and module contexts in the wrong way. I had this error yesterday while working on a gem which extends deep inner workings of ActiveRecord. I finally managed to get around this problem by redesigning my code which exploits the inner contexts. It would be interesting to see the surrounding lines of environment.rb:66 for further analysis.
This may happen when the class name doesn't match the filename, in
my case it was a file named application.rb contaning the ApplicationController
class. Renaming the file to application_controller.rb solved the problem.
When I got this error, it was due to a misspelling while defining a class. If you are getting this error, it may be worth examining your module and class definitions for typos.

Resources