I recently upgraded a bunch of gems, and since then I can't run rails server, console, or rspec.
Here's the error log I see:
NoMethodError:
undefined method 'name' in 'book' factory
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/definition_proxy.rb:97:in `method_missing'
# ./spec/factories/books.rb:3:in `block (2 levels) in <top (required)>'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/syntax/default.rb:18:in `instance_eval'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/syntax/default.rb:18:in `factory'
# ./spec/factories/books.rb:2:in `block in <top (required)>'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/syntax/default.rb:49:in `instance_eval'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/syntax/default.rb:49:in `run'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/syntax/default.rb:7:in `define'
# ./spec/factories/books.rb:1:in `<top (required)>'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/find_definitions.rb:20:in `block (2 levels) in find_definitions'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/find_definitions.rb:19:in `each'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/find_definitions.rb:19:in `block in find_definitions'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/find_definitions.rb:15:in `each'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/find_definitions.rb:15:in `find_definitions'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/reload.rb:6:in `reload'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot_rails-5.0.2/lib/factory_bot_rails/reloader.rb:25:in `block in build_reloader'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot_rails-5.0.2/lib/factory_bot_rails/reloader.rb:35:in `block in register_reloader'
# ./config/environment.rb:5:in `<top (required)>'
# ./spec/rails_helper.rb:3:in `require'
# ./spec/rails_helper.rb:3:in `<top (required)>'
# ./spec/controllers/api/v1/songs_controller_spec.rb:1:in `require'
# ./spec/controllers/api/v1/songs_controller_spec.rb:1:in `<top (required)>'
Part of the upgrade included upgrading rails to 5.2.3, which forced me to remove this line from new_framework_defaults.rb file:
# Do not halt callback chains when a callback returns false. Previous versions had true.
ActiveSupport.halt_callback_chains_on_return_false = false
I'm guessing this has something to do with it?
Edit: The factory code is below. This exact code was working 100% before upgrading the gems:
FactoryBot.define do
factory :book do
name "my songbook"
end
end
FactoryBot changed its syntax. It now expects a block when assigning values – even static values – to variables. Change your code to this:
FactoryBot.define do
factory :book do
name { "my songbook" }
end
end
See Defining Factories in their docs.
Related
I have a controller SocketController inherit WebsocketRails::BaseController
class SocketController < WebsocketRails::BaseController
before_action :authenticate_user!
end
After upgrade rails version to 7.0.4
When I run rails s got this error
How to fix it, please!!
Failure/Error:
class SocketController < WebsocketRails::BaseController
before_action :authenticate_user!
NoMethodError:
undefined method `unloadable' for WebsocketRails::BaseController:Class
unloadable controller
^^^^^^^^^^
# ./vendor/bundle/ruby/3.1.0/gems/websocket-rails-0.7.0/lib/websocket_rails/base_controller.rb:45:in `inherited'
# ./app/controllers/socket_controller.rb:1:in `<top (required)>'
# ./vendor/bundle/ruby/3.1.0/gems/zeitwerk-2.6.6/lib/zeitwerk/kernel.rb:30:in `require'
# ./vendor/bundle/ruby/3.1.0/gems/zeitwerk-2.6.6/lib/zeitwerk/kernel.rb:30:in `require'
# ./vendor/bundle/ruby/3.1.0/gems/zeitwerk-2.6.6/lib/zeitwerk/loader/helpers.rb:135:in `const_get'
# ./vendor/bundle/ruby/3.1.0/gems/zeitwerk-2.6.6/lib/zeitwerk/loader/helpers.rb:135:in `cget'
# ./vendor/bundle/ruby/3.1.0/gems/zeitwerk-2.6.6/lib/zeitwerk/loader/eager_load.rb:169:in `block in actual_eager_load_dir'
# ./vendor/bundle/ruby/3.1.0/gems/zeitwerk-2.6.6/lib/zeitwerk/loader/helpers.rb:40:in `block in ls'
# ./vendor/bundle/ruby/3.1.0/gems/zeitwerk-2.6.6/lib/zeitwerk/loader/helpers.rb:25:in `each'
# ./vendor/bundle/ruby/3.1.0/gems/zeitwerk-2.6.6/lib/zeitwerk/loader/helpers.rb:25:in `ls'
# ./vendor/bundle/ruby/3.1.0/gems/zeitwerk-2.6.6/lib/zeitwerk/loader/eager_load.rb:164:in `actual_eager_load_dir'
# ./vendor/bundle/ruby/3.1.0/gems/zeitwerk-2.6.6/lib/zeitwerk/loader/eager_load.rb:17:in `block (2 levels) in eager_load'
# ./vendor/bundle/ruby/3.1.0/gems/zeitwerk-2.6.6/lib/zeitwerk/loader/eager_load.rb:16:in `each'
# ./vendor/bundle/ruby/3.1.0/gems/zeitwerk-2.6.6/lib/zeitwerk/loader/eager_load.rb:16:in `block in eager_load'
# ./vendor/bundle/ruby/3.1.0/gems/zeitwerk-2.6.6/lib/zeitwerk/loader/eager_load.rb:10:in `synchronize'
# ./vendor/bundle/ruby/3.1.0/gems/zeitwerk-2.6.6/lib/zeitwerk/loader/eager_load.rb:10:in `eager_load'
# ./vendor/bundle/ruby/3.1.0/gems/zeitwerk-2.6.6/lib/zeitwerk/loader.rb:296:in `block in eager_load_all'
# ./vendor/bundle/ruby/3.1.0/gems/zeitwerk-2.6.6/lib/zeitwerk/loader.rb:294:in `each'
# ./vendor/bundle/ruby/3.1.0/gems/zeitwerk-2.6.6/lib/zeitwerk/loader.rb:294:in `eager_load_all'
# ./vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/application/finisher.rb:74:in `block in <module:Finisher>'
# ./vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/initializable.rb:32:in `instance_exec'
# ./vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/initializable.rb:32:in `run'
# ./vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/initializable.rb:61:in `block in run_initializers'
# ./vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/initializable.rb:60:in `run_initializers'
# ./vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/application.rb:372:in `initialize!'
# ./config/environment.rb:5:in `<top (required)>'
I think:
Could it be that 1 gem version or 1 configuration is not suitable, causing it to fail?
I am not sure what is that for, your controller is reloadable because it is in app/controllers, like any other Ruby file in those directories.
That gem has not had activity for years. I'd suggest that you reopen WebsocketRails::BaseController in an initializer, define a dummy unloadable class method in it that does nothing, and see if your controllers reload as expected.
Should my app be calling Dotenv.load when its running its test suite?
Without calling Dotenv.load while running my test suite, rspec fails with an error about missing an environment key. I have pasted the error and its stack trace below.
$ rspec
An error occurred while loading ./spec/requests/users/create_spec.rb.
Failure/Error: require File.expand_path('../../config/environment', __FILE__)
KeyError:
key not found: "REDIS_URL"
# (erb):11:in `fetch'
# (erb):11:in `<main>'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/application.rb:233:in `config_for'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/actioncable-5.1.4/lib/action_cable/engine.rb:31:in `block (2 levels) in <class:Engine>'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:69:in `instance_eval'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:69:in `block in execute_hook'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:60:in `with_execution_control'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:65:in `execute_hook'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:50:in `block in run_load_hooks'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:49:in `each'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:49:in `run_load_hooks'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/actioncable-5.1.4/lib/action_cable/server/base.rb:85:in `<module:Server>'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/actioncable-5.1.4/lib/action_cable/server/base.rb:4:in `<module:ActionCable>'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/actioncable-5.1.4/lib/action_cable/server/base.rb:3:in `<top (required)>'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `block in require'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:258:in `load_dependency'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/actioncable-5.1.4/lib/action_cable.rb:44:in `server'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/actioncable-5.1.4/lib/action_cable/engine.rb:46:in `block (3 levels) in <class:Engine>'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/actionpack-5.1.4/lib/action_dispatch/routing/route_set.rb:426:in `instance_exec'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/actionpack-5.1.4/lib/action_dispatch/routing/route_set.rb:426:in `eval_block'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/actionpack-5.1.4/lib/action_dispatch/routing/route_set.rb:443:in `block in clear!'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/actionpack-5.1.4/lib/action_dispatch/routing/route_set.rb:443:in `each'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/actionpack-5.1.4/lib/action_dispatch/routing/route_set.rb:443:in `clear!'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/application/routes_reloader.rb:50:in `block in clear!'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/application/routes_reloader.rb:48:in `each'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/application/routes_reloader.rb:48:in `clear!'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/application/routes_reloader.rb:17:in `reload!'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/application/routes_reloader.rb:41:in `block in updater'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/file_update_checker.rb:81:in `execute'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/application/routes_reloader.rb:42:in `updater'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/application/routes_reloader.rb:31:in `execute_if_updated'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/application/finisher.rb:128:in `block in <module:Finisher>'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/initializable.rb:30:in `instance_exec'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/initializable.rb:30:in `run'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/initializable.rb:59:in `block in run_initializers'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/initializable.rb:58:in `run_initializers'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/application.rb:353:in `initialize!'
# ./config/environment.rb:5:in `<top (required)>'
# ./spec/rails_helper.rb:4:in `require'
# ./spec/rails_helper.rb:4:in `<top (required)>'
# ./spec/requests/users/create_spec.rb:1:in `require'
# ./spec/requests/users/create_spec.rb:1:in `<top (required)>'
Outside of a Rails app you want to do the following:
require 'dotenv/tasks'
task start: [:dotenv] do
# this Rake tasks depends on the dotenv loading task
puts 'hi'
end
Inside a Rails app:
Since you're actually in a Rails app, I would make sure you're using dotenv-rails in your Gemfile, instead of just dotenv. The former will auto run the dotenv stuff as part of the Rails startup routine
I have a factory girl factory defined as follows:
FactoryGirl.define do
factory :task do
factory :facebook_task, class: Task::Facebook do
# ...
end
factory :twitter_task, class: Task::Twitter do
# ...
end
end
end
for models defined as follows:
class Task < ActiveRecord::Base
end
class Task::Twitter < Task
end
class Task::Facebook < Task
end
It all work fine until I add the twitter gem which clearly defines a ::Twitter module
.../test/factories/tasks.rb:12: warning: toplevel constant Twitter referenced by Task::Twitter
Running via Spring preloader in process 51820
rake aborted!
NoMethodError: undefined method `new' for Twitter:Module
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator/new_constructor.rb:9:in `new'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator.rb:14:in `send'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator.rb:10:in `method_missing'
Cleaning database...done.
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator/invocation_tracker.rb:11:in `method_missing'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator.rb:14:in `send'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator.rb:10:in `method_missing'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/configuration.rb:19:in `block in initialize'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:48:in `instance_exec'
Creating Beatles.../Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:48:in `build_class_instance'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:13:in `object'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/evaluation.rb:12:in `object'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/strategy/create.rb:9:in `result'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/factory.rb:42:in `run'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/factory_runner.rb:29:in `block in run'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/notifications.rb:166:in `instrument'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/factory_runner.rb:28:in `run'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
/Users/pupeno/Documents/code/buzz/lib/tasks/sample_data.rake:25:in `block (2 levels) in <top (required)>'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `block in load'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
-e:1:in `<main>'
Tasks: TOP => sample_data:generate
(See full trace by running task with --trace)
Why is Factory Girl trying to instantiate Twitter instead of Task::Twitter?
tasks.rb:12 is this line:
factory :twitter_task, class: Task::Twitter do
I don't see how it's referencing the toplevel constant Twitter.
try to use class name as a string:
factory :facebook_task, class: 'Task::Facebook' do
# ...
end
instead
factory :facebook_task, class: Task::Facebook do
# ...
end
I have encountered a strange error with a custom attribute validator. I had the following setup and everything worked as expected (every class is wrapped with module API and V1):
# app/models/api/v1/business_account.rb
class BusinessAccount
has_many :transactions, class_name: BusinessAccountThings::Transaction
end
# app/models/api/v1/business_account_things/transaction.rb
module BusinessAccountThings
class Transaction
belongs_to :business_account
validates :amount, money: true
end
end
# app/validators/api/v1/money_validator.rb
class MoneyValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless /^\d+\.*\d{0,2}$/ =~ value.to_s
record.errors[attribute] << 'Only two digits are allowed.'
end
end
end
I now want to use the custom validator in BusinessAccount, like this:
class BusinessAccount
has_many :transactions
validates :current_balance, money: true
end
I then get the following error when running the spec for BusinessAccount (and a similar error when trying to load the class from Rails console):
/home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activemodel-4.2.4/lib/active_model/validations/validates.rb:120:in `rescue in block in validates': Unknown validator: 'MoneyValidator' (ArgumentError)
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activemodel-4.2.4/lib/active_model/validations/validates.rb:117:in `block in validates'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activemodel-4.2.4/lib/active_model/validations/validates.rb:113:in `each'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activemodel-4.2.4/lib/active_model/validations/validates.rb:113:in `validates'
from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account_things/transaction.rb:22:in `<class:Transaction>'
from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account_things/transaction.rb:5:in `<module:BusinessAccountThings>'
from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account_things/transaction.rb:3:in `<module:V1>'
from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account_things/transaction.rb:2:in `<module:API>'
from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account_things/transaction.rb:1:in `<top (required)>'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `block in require'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:360:in `require_or_load'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:494:in `load_missing_constant'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:184:in `const_missing'
from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account.rb:16:in `<class:BusinessAccount>'
from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account.rb:4:in `<module:V1>'
from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account.rb:2:in `<module:API>'
from /home/hannes/workspaces/rails-app/app/models/api/v1/business_account.rb:1:in `<top (required)>'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `block in require'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:360:in `require_or_load'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:494:in `load_missing_constant'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:184:in `const_missing'
from /home/hannes/workspaces/rails-app/spec/models/api/v1/business_account_spec.rb:5:in `<top (required)>'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1361:in `load'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1361:in `block in load_spec_files'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1359:in `each'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1359:in `load_spec_files'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:106:in `setup'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:92:in `run'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:78:in `run'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:45:in `invoke'
from /home/hannes/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/exe/rspec:4:in `<top (required)>'
from /home/hannes/.rbenv/versions/2.2.5/bin/rspec:23:in `load'
from /home/hannes/.rbenv/versions/2.2.5/bin/rspec:23:in `<main>'
Line 16 in BusinessAccount is has_many :transactions, class_name: BusinessAccountThings::Transaction, dependent: :destroy
Line 22 in Transaction is validates :amount, money: true.
If I remove Line 22 from Transaction, the problem disappears.
If I run my spec for Transaction, I get this error:
1) API::V1::BusinessAccountThings::Transaction
Failure/Error:
validates :current_balance,
numericality: { greater_than_or_equal_to: 0 },
money: true
ArgumentError:
Unknown validator: 'MoneyValidator'
# ./app/models/api/v1/business_account.rb:9:in `<class:BusinessAccount>'
# ./app/models/api/v1/business_account.rb:4:in `<module:V1>'
# ./app/models/api/v1/business_account.rb:2:in `<module:API>'
# ./app/models/api/v1/business_account.rb:1:in `<top (required)>'
# ./spec/models/api/v1/business_account_things/transaction_spec.rb:5:in `block (2 levels) in <top (required)>'
# ./spec/models/api/v1/business_account_things/transaction_spec.rb:7:in `block (2 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# NameError:
# uninitialized constant API::V1::BusinessAccount::MoneyValidator
# ./app/models/api/v1/business_account.rb:9:in `<class:BusinessAccount>'
So it appears as if both classes cannot load the MoneyValidator at the same time... How can I solve this?
Autoloading API::V1::MoneyValidator fails with your setup.
business_account.rb is trying to load it as API::V1::BusinessAccount::MoneyValidator. That fails because it is namespaced under API::V1, not API::V1::BusinessAccount.
Autoloading will also try to find MoneyValidator (without namespace; as written in the source code). So if you move your validator class out of the API::V1 namespace and make it a top-level constant, then it should work.
I'm getting some strange results from view specs
Failure/Error: assert_select "div.fileinput-new.thumbnail"
ArgumentError:
wrong number of arguments (3 for 1)
Ok, so this seems self explanatory (though I'm not sure what are the 3 arguments referenced).
However, if I run the suite again, I might get 0 errors. And the next, 5 errors, of the same type as above but referencing different DOM elements.
There is nothing fancy in any of these specs
describe do
build :object / create :object
#object = assign(:object, object )
it 'displays the DOM elements' do
render
asset_select #DOM_elements
end
end
What could cause this kind of inconsistent coverage of view specs?
An example stack trace
Failure/Error: assert_select "div.fileinput-new.thumbnail"
ArgumentError:
wrong number of arguments (3 for 1)
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/actionview-4.2.6/lib/action_view/helpers/text_helper.rb:52:in `concat'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/searchable.rb:165:in `evaluate'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/searchable.rb:165:in `block in xpath'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/searchable.rb:156:in `map'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/searchable.rb:156:in `xpath'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/searchable.rb:193:in `css_internal'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/node_set.rb:76:in `block in css'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/node_set.rb:187:in `block in each'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/node_set.rb:186:in `upto'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/node_set.rb:186:in `each'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/node_set.rb:75:in `inject'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/node_set.rb:75:in `css'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rails-dom-testing-1.0.7/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb:20:in `select'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rails-dom-testing-1.0.7/lib/rails/dom/testing/assertions/selector_assertions.rb:174:in `assert_select'
# ./spec/views/users/settings/profile.html.erb_spec.rb:24:in `block (4 levels) in <top (required)>'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rails-dom-testing-1.0.7/lib/rails/dom/testing/assertions/selector_assertions.rb:295:in `nest_selection'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rails-dom-testing-1.0.7/lib/rails/dom/testing/assertions/selector_assertions.rb:177:in `block in assert_select'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rails-dom-testing-1.0.7/lib/rails/dom/testing/assertions/selector_assertions.rb:174:in `tap'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rails-dom-testing-1.0.7/lib/rails/dom/testing/assertions/selector_assertions.rb:174:in `assert_select'
# ./spec/views/users/settings/profile.html.erb_spec.rb:22:in `block (3 levels) in <top (required)>'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:236:in `instance_exec'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:236:in `block in run'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:478:in `block in with_around_and_singleton_context_hooks'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:435:in `block in with_around_example_hooks'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/hooks.rb:478:in `block in run'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/hooks.rb:618:in `block in run_around_example_hooks_for'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:320:in `call'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:320:in `call'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-rails-3.4.2/lib/rspec/rails/adapters.rb:127:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:425:in `instance_exec'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:425:in `instance_exec'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/hooks.rb:389:in `execute_with'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/hooks.rb:620:in `block (2 levels) in run_around_example_hooks_for'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:320:in `call'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:320:in `call'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/hooks.rb:621:in `run_around_example_hooks_for'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/hooks.rb:478:in `run'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:435:in `with_around_example_hooks'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:478:in `with_around_and_singleton_context_hooks'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:233:in `run'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:581:in `block in run_examples'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:577:in `map'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:577:in `run_examples'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:543:in `run'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:544:in `block in run'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:544:in `map'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:544:in `run'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:119:in `block (3 levels) in run_specs'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:119:in `map'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:119:in `block (2 levels) in run_specs'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1680:in `with_suite_hooks'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:118:in `block in run_specs'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/reporter.rb:77:in `report'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:117:in `run_specs'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:93:in `run'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:78:in `run'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:45:in `invoke'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/gems/rspec-core-3.4.4/exe/rspec:4:in `<top (required)>'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/bin/rspec:23:in `load'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/bin/rspec:23:in `<main>'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/bin/ruby_executable_hooks:15:in `eval'
# /Users/me/.rvm/gems/ruby-2.2.3#myapp/bin/ruby_executable_hooks:15:in `<main>'
This is not a definitive answer: it appears that, for some weird reason, the evaluate method in the Nokogiri's Searchable module considers the concat() function as a custom XPath function instead of the internal XPath function.
External XPath functions are called as normal ruby methods in the context of the handler attribute of evaluate. Normally the handler is the SubstitutionContext class but in your case it seems that sometimes this gets the context of the TextHelper module where the concat method is defined, accepting just 1 parameter (whereas the XPath's concat() function accepts any number of parameters). I think that this can lead to the errors you observe.
Could you open the searchable.rb file (see the stack trace for its location) on this line and add before it some debug messages to inspect the contents of the following variables?
puts "ctx #{ctx.inspect}"
puts "path #{path.inspect}"
puts "handler #{handler.inspect}"
if handler.respond_to?(:concat)
puts "concat #{handler.method(:concat).inspect}"
else
puts "not responding to :concat"
end
Then, can you provide the output printed for one of the failing tests?
Update: the output showed that indeed the tests context was being polluted by the ActionView::TextHelpers module and in the end it turned out that there was a ActionView::Helpers module included in a model used in the tests.