Joe Gatt
Joe Gatt

Reputation: 2235

Rails Rspec warning: "This dynamic method is deprecated."

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.

Upvotes: 3

Views: 799

Answers (2)

declan
declan

Reputation: 5645

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.

Upvotes: 1

Michael Stalker
Michael Stalker

Reputation: 1367

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?

Upvotes: 0

Related Questions