Unable to use rspec and rollbar after upgrading to rails 5.
Create a Rails 4 app
Upgrade gemfile to use rails 5
Try adding rollbar gem/support
Standard config/environment.rb:
# Load the Rails application.
require_relative 'application'
# Initialize the Rails application.
Rails.application.initialize!
Error when running rspec:
An error occurred while loading {path to specific spec file}
Failure/Error: require File.expand_path('../../config/environment', __FILE__)
RuntimeError:
can't modify frozen Array
# ./config/environment.rb:6:in `<top (required)>'
# ./spec/rails_helper.rb:5:in `<top (required)>'
...
No examples found.
In most cases, that error is a red herring for something else.
When encountering it, don't get overwhelmed with the recurrent can't modify frozen Array error messages, and instead check the very first error that appears when running a spec.
For example:
Failure/Error: validate :uniqueness, if: 'should_be_unique?'
ArgumentError: Passing string to be evaluated in :if and :unless
conditional options is not supported. Pass a symbol for an instance
method, or a lambda, proc or block, instead.
Just to add one tip on top of Maxximo Mussini's answer.
If anyone can't find the first error on the terminal, please try to run RSpec on one file, i.e. rspec spec/models/user_spec.rb
You should be able to find the root case.
In my case, I haven't updated my local .env variables that is required by User model
Hope it helps
Debugging this is not easy but one possible solution is simple. It could be a naming conflict with Rollbar, possibly something getting monkey-patched. If you're seeing this RuntimeError but not using Rollbar, see other answer.
Add a Module ("namespace" of your choice) around your app class definition in config/application.rb.
The module won't affect much. The only difference I could find is when printing out your app it will now appear as (that's how we found the fix vs a new working app):
<MyTestAPP::Application ...> instead of <Application ...>
Change:
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1
end
To:
Module MyTestApp
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1
end
end
Related
I've recently started the upgrading process of my app to use Rails 6. Some of it is very smooth, some of it not.
The problem I am facing right now is that in a factory bot we define:
factory :webhook_endpoint, class: Webhook::Endpoint do
...
end
When running rails zeitwerk:check it simply throws Uninitialized constant WebHookEndpoint.
The module and class is defined in the models folder.
I wonder I need to require or include it for it to realize it exists.
I recently upgraded to Rails 5 and am encountering an error on development that is (what I expect, anyway) related to loading a shared module.
The error is Rspec: NameError: uninitialized constant ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES and occurs when running Rspec tests that use a method that uses a shared module.
The path for the shared module is: lib/shared/truthy.rb and the module code is below.
module Shared
module Truthy
def self.boolean(value)
ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(value)
end
end
end
The path for the file that uses the method that accesses this module is: engines/events/app/controllers/events/events_controller.rband the method that uses the shared module is below.
def official_event?
Shared::Truthy.boolean(params[:event][:official])
end
This wasn't throwing any error prior to upgrading to Rails 5, so it's safe to say there's no issue with the Rspec test.
Thanks in advance for any input.
I found out that this error is a result of Rails 5 removing uses of ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.
I have activemerchant 1.16.0 and rails 3.0.5.
I am trying to build a basic code to communicate with PayPal's gateway using active merchant.
if credit_card.valid?
# or gateway.purchase to do both authorize and capture
response = gateway.authorize(1000, credit_card, :ip => "127.0.0.1")
if response.success?
gateway.capture(1000, response.authorization)
puts "Purchase complete!"
else
puts "Error: #{response.message}"
end
else
puts "Error: credit card is not valid. #{credit_card.errors.full_messages.join('. ')}"
end
I get the following error:
/Library/Ruby/Gems/1.8/gems/activesupport-3.0.9/lib/active_support/xml_mini/rexml.rb:20:in `parse': uninitialized constant ActiveSupport::XmlMini_REXML::StringIO (NameError)
This error propagates from the gateway.authorize() call.
Any idea what's wrong with my setup?
Thanks.
According to the question, it doesn't work when the code is by itself, but works when require "stringio" is added.
My suspicion is that ActiveMerchant is unit-tested, but for some reason the dependency on StringIO isn't detected by those unit tests, possibly because other parts of the unit testing code indirectly requires stringio.
One thing I recently found out was that require 'yaml' gives you the stringio library as a side effect:
StringIO.new
# NameError: uninitialized constant StringIO
# from (irb):1
require "yaml"
# => true
StringIO.new
# => #<StringIO:0x7fb7d48ce360>
RUBY_VERSION
# => "1.8.7"
and it wouldn't be hard to imagine unit tests for ActiveMerchant (or other parts of Rails) requiring yaml.
However, this is only speculation. I haven't checked, as I don't use Rails.
Andrew Grimm pretty much hit the nail on the head with his original comment on this question. The missing require 'stringio' is indeed the issue. But it is a bug with Rails, more specifically ActiveSupport 3.0.9 (which is where the error seems to be coming from). We can trace it down using the git commit history of rails.
First we need to check out rails and switch to the v3.0.9 tag. If we now look at activesupport/lib/active_support/xml_mini/rexml.rb we see that require 'stringio' is not there. In and of itself this is not significant, but bear with me. We can now switch to the next tag (v3.0.10.rc1), and we'll see that the file hasn't been updated (it is likely that this version of rails will have the same issue). Next tag in line is v3.1.0.beta1, notice that this time around there is a require 'stringio' at the top of the file.
We can check out the commit that brought in this change (this one here from Jan 19th 2011). The commit message says:
fixed a missing require that causes trouble when using AS in a
non-rails env
This would indicate that as long as you're in a rails environment this issue wouldn't surface. So, my guess is something about the environment caused the issue to come up, may have something to do with the fact that the OP says they are using rails 3.0.5, but the error is coming from activesupport-3.0.9. Perhaps the code was called from a rake task that forgot to inherit from :environment (difficult to say without more info). Either way, putting require 'stringio' at the top of the code is definitely the fix, until you can upgrade to Rails 3.1 (once it comes out) at which point the require will no longer be needed.
In order to override the table_exists? method in the Rails PostgreSQL adapter I have tried the following in an initializer file:
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
def table_exists?(name)
raise 'got here'
end
end
This will raise the the following error:
uninitialized constant ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
I believe this would have worked in previous versions of rails and I even found a small plugin that did something like this in Rails 2.3.6. Also I only encounter this error when I am trying to run a rake task like db:migrate and not when I start my application server.
Could someone show me the right way to do this and / or explain why PostgreSQLAdapter doesn't seem to be loaded when I am in an initializer file?
Instead of config/initializers, place that code in lib/ folder.
While this means that the active_record is loaded after the rails initializers, which is unusual. I ll update this with more detail, once I am done investigating the whole flow. If you want some more details about the rails 3 initialization process, check out this link:
http://ryanbigg.com/guides/initialization.html
I had success by moving this code into a Rails plugin. It is a little bit more overhead, but it is working consistently when I run rails s and when I run rake db:migrate.
I just followed the rails guide page on the topic and ran
rails generate plugin rails_patches --with-generator
and moved my init.rb file into rails as recommended.
~vendor/
`~plugins/
`~rails_patches/
|~lib/
| `-rails_patches.rb
|~rails/
| `-init.rb
|+test/
|-install.rb
|-MIT-LICENSE
|-Rakefile
|-README
`-uninstall.rb
I put this code in init.rb:
require 'rails_patches'
I put this code in rails_patches.rb:
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
def table_exists?(name)
raise 'got here'
end
end
This now behaves as I expected.
What's the Rails 3 replacement for ActiveRecord::Errors?
In Rails 2.3.8, this is an object:
>> ActiveRecord::Errors
=> ActiveRecord::Errors
In Rails 3.0.0rc, you get a NameError:
>> ActiveRecord::Errors
NameError: uninitialized constant ActiveRecord::Errors
from (irb):2
I'm trying to make the wizardly generator work with Rails 3.
$ rails g wizardly_scaffold home
But it fails:
/Library/Ruby/Gems/1.8/gems/wizardly_gt-0.1.8.9/lib/validation_group.rb:150:
uninitialized constant ActiveRecord::Errors (NameError)
The line it refers to is this:
ActiveRecord::Errors.send :include, ValidationGroup::ActiveRecord::Errors
Earlier in the file, we see:
module ValidationGroup
module ActiveRecord
...
module Errors # included in ActiveRecord::Errors
def add_with_validation_group(attribute, msg = I18n.translate('activerecord.errors.messages')[:invalid], *args, &block)
add_error = #base.respond_to?(:should_validate?) ? (#base.should_validate?(attribute.to_sym) || attribute == :base) : true
add_without_validation_group(attribute, msg, *args, &block) if add_error
end
...
end
That'd be ActiveModel::Errors. Things such as validations and error handling have been moved over to Active Model to provide a common API for all ORM Railties such as Active Record, Data Mapper, Mongoid etc. to hook into Rails with.
It would appear the wizardly plugin needs to check for ActiveModel first and if it exists, then include the error handling there rather than ActiveRecord::Errors. A trivial change.
Try this gem
http://rubygems.org/gems/wizardly_gt
I have only just begin playing with wizardly, but the above at least seems to be compatible with Rails 3.
Wizardly obviously does a lot more, but you should check out validation_scopes, which I just updated for Rails3 compatibility. Rather than grouping things by attributes, it just lets you explicitly declare different groups of validations by creating namespaced collections of errors. Internally it's a much simpler implementation (the same code handles Rails 2 and 3). Personally I find this to be more flexible than grouping by attribute (what if an attribute should have different constraints in different steps of a wizard for instance?).