5

Recently I have updated Ruby(2.5.3 to 2.7.1) and Ruby on rails(5.2.2 to 6.1.1) version.

After that when I run rspec, I got this error:

Failure/Error: require File.expand_path("../../config/environment", __FILE__)
NoMethodError:
  undefined method `new' for BigDecimal:Class
# ./config/application.rb:11:in `<top (required)>'
# ./config/environment.rb:2:in `<top (required)>'

An error occurred in spec_helper.rb How to debug spec_helper's code which is:

require File.expand_path("../../config/environment", __FILE__)

how to resolve the above error? I'm not sure which gem/file trying to do BigDecimal.new

Vishal
  • 818
  • 8
  • 20
  • 1
    A quick lesson to be learned here is, if an update goes wrong, you can try updating **one thing at a time** to find the true source of the problem. You might have updated 100 different libraries here, for all I know, and you've jumped multiple versions at once. Try updating just-ruby, to 2.6. Then try updating just-ruby to 2.7. Then try updating just-rails to 6.0 .... etc etc. – Tom Lord Jan 18 '21 at 18:59
  • Can you share your Gemfile here. – Bijendra Jan 18 '21 at 21:04

2 Answers2

6

As suggested by @Tom Lord in the comments updating shoulda matcher to 3.1.3 worked for me

Aniket Tiwari
  • 3,561
  • 4
  • 21
  • 61
0

Requiring config/environment.rb is just loading rails and your app. Most likely one of your dependencies is not compatible with ruby 2.6 (for this exact error with BigDecimal.new there's explanation in this answer here, but it is not guaranteed to be the only one), rails by default filters gems from backtraces.

To debug this you need unfiltered error trace. To get that you can wrap Bundler.require(*Rails.groups) and Application.initialize! (most likely the error will be in an initializer) with a rescue and get the backtrace from exception directly.

Other approach is to upgrade everything reported by bundle outdated since you're already taking the risk of jumping over multiple rails versions and hope that all used gems already released a compatible version.

PS. usually it's not recommended to skip minor rails and ruby versions during upgrade, because this way you can skip some important deprecations and get unexpected behavior

Vasfed
  • 18,013
  • 10
  • 47
  • 53
  • thanks for showing your interest. Actually, I have used next_rails gem and checked each gem's compatibility with the 6.1 also I'm able to run rails server and console but I'm getting this error when I run any unit test cases with RSpec command – Vishal Jan 18 '21 at 17:38
  • also after wrapping the code in begin-rescue, it gives me the same error which is: undefined method `new' for BigDecimal:Class – Vishal Jan 18 '21 at 17:39
  • 1
    @Vishal you're interested in backtrace from the error. If the error does not occur in `development` env - then it may be some of the gems in `test` group. Incompatibility is not with rails, but with ruby – Vasfed Jan 18 '21 at 17:41
  • `BigDecimal.new` was deprecated in ruby 2.6, so another suggestion would be to *run the application using ruby 2.6* (yes, this means you'll need to install all the libraries unfortunately...) and look for the source of the noisy deprecation warnings. – Tom Lord Jan 18 '21 at 18:55
  • 3
    A *guess* - and this really is a complete guess - is that you need to [update the `shoulda-matchers` gem](https://github.com/thoughtbot/shoulda-matchers/issues/1166) to [version `3.1.3` or above](https://github.com/thoughtbot/shoulda-matchers/blob/master/CHANGELOG.md#313---2019-01-29). – Tom Lord Jan 18 '21 at 18:57
  • @TomLord updating shoulda matcher worked!!! thanks a lot – Vishal Jan 19 '21 at 05:27