Rspec: undefined method `new' error in ActiveRecord model - ruby-on-rails

I know this has to be something stupid, but I keep getting the following error in one of my examples:
undefined method `new' for #<Class:0x211d274>
I have created a simple example to show the error:
describe LateCharge do
before :each do
#membership = Membership.new
#location = mock_model(Location, :late_payment_rate => 10)
end
it "should initialize" do
LateCharge.respond_to?('new').should == true
#charge = LateCharge.new(#membership, #location)
end
end
The strange part is, when I run the example by itself, it passes. When I run it with all my examples, it fails with the following error:
NoMethodError in 'LateCharge should initialize'
undefined method `new' for #<Class:0x211d274>
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1964:in `method_missing_without_paginate'
/Users/l33/.gem/ruby/1.8/gems/mislav-will_paginate-2.3.11/lib/will_paginate/finder.rb:170:in `method_missing'
./spec/models/late_charge_spec.rb:15:
It is failing on the line: #charge = LateCharge.new(#membership, #location)
I do not have any problems instantiating the LateCharge object at run time or from the console.
Anyone have any ideas?

Seems to me the following information is key to your issue:
will_paginate/finder.rb:170:in `method_missing'

Hey Lee - not sure if you're still having this problem, but I had the exact same thing and it's because another spec I had was unstubbing the function.

Related

Gettting undefined method `empty?' for nil:NilClass in my test suite when I try to call a Rails path helper (*_path) using Rails, Rspec

Rails 6.1.3.1
Rspec
basic behavior spec code:
describe "index" do
it "should show me the list" do
visit dashboard_targets_path
end
end
the routes file
namespace :dashboard do
resources :targets
end
error shows me the exception, but strangely it appears as if it isn't calling through to the app, just fails right in my test code:
1) interaction for Dashboard::TargetsController index should show me the list
Failure/Error: visit dashboard_targets_path
NoMethodError:
undefined method `empty?' for nil:NilClass
# ./spec/system/dashboard/targets_behavior_spec.rb:16:in `block (3 levels) in <top (required)>'
# /Users/jason/.rvm/gems/ruby-2.6.6/gems/webmock-3.12.2/lib/webmock/rspec.rb:37:in `block (2 levels) in <main>'
# /Users/jason/.rvm/gems/ruby-2.6.6/gems/rspec-wait-0.0.9/lib/rspec/wait.rb:46:in `block (2 levels) in <main>'
it seems to be failing inside the test code, if I drop into the debugger there and run dashboard_targets_path directly I also get the same exception, so the problem is just using the helper within the TEST ENVIRONMENT
within the dev environment, this function works
the problem here was the the config/environments/test.rb file does have default_url_options.
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
when you hit errors that disappear into the Rails gems, a good way to debug them is like so:
begin
// your failing code here
rescue StandardError => e
puts e.backtrace
byebug
puts e.inspect
raise (e)
end
WARNING: do not leave this code in your app or check it in unless you explicitly want to use exception handling for flow control (not recommended!). This is recommended ONLY for debugging purposes.
here you will see the full backtrace to the line number in the Gem where it is failing. when debugging Gems be careful— remember to un-do any changes you make and know that your monkey-patching inside of the Gem code doesn't affect your production code.

rails admin undefined method `new' for nil:NilClass error

I'm getting an error message when I try to go inside the admin dashboard by the rails_admin gem.
Error
undefined method `new' for nil:NilClass
This is the whole code
klass.setup if klass.respond_to? :setup
#authorize = proc do
#authorization_adapter = klass.new(*([self] + args).compact)
end
elsif block
#authorize = block
It is pointing to this spot
#authorization_adapter = klass.new(*([self] + args).compact)
I don't know why this happening because I did not touch anything from the rails admin
To anyone who is scratching their head over this issue and stumbled upon this thread, on /config/initializers/rails_admin.rb try replacing
config.authorize_with :cancan
with
config.authorize_with :cancancan

Rails_admin undefined method `associations' for nil:NilClass

I have these models:
Class A
embeds_many :b
end
Class B
belongs_to :c
end
Class C
end
I'm working with rails_admin and mongoid. In admin, when I try to retrieve the list of C records when I'm creating an A instance I'm getting this error:
This only happens on production envirnment not in development
NoMethodError (undefined method `associations' for nil:NilClass):
/home/pablo/.rvm/gems/ruby-2.3.0#mh-backend/bundler/gems/rails_admin-355dc80f8a20/lib/rails_admin/adapters/mongoid/abstract_object.rb:10:in `initialize'
/home/pablo/.rvm/gems/ruby-2.3.0#mh-backend/bundler/gems/rails_admin-355dc80f8a20/lib/rails_admin/adapters/mongoid.rb:24:in `new'
/home/pablo/.rvm/gems/ruby-2.3.0#mh-backend/bundler/gems/rails_admin-355dc80f8a20/lib/rails_admin/adapters/mongoid.rb:24:in `get'
/home/pablo/.rvm/gems/ruby-2.3.0#mh-backend/bundler/gems/rails_admin-355dc80f8a20/app/controllers/rails_admin/main_controller.rb:138:in `get_association_scope_from_params'
Taking a look at rails_admin code we can see that piece of code in mongoid.rb file.
def get(id)
AbstractObject.new(model.find(id))
rescue => e
raise e if %w(
Mongoid::Errors::DocumentNotFound
Mongoid::Errors::InvalidFind
Moped::Errors::InvalidObjectId
BSON::InvalidObjectId
).exclude?(e.class.to_s)
end
If we pay attention at this code we can see that model.find(id) must produce a Mongoid::Errors::DocumentNotFound if document doesn't exists by default.
However, in mongoid you can avoid the raise of this error with raise_not_found_error: true in mongo config file, this produce the undefined method of nil class.
Tracking issue on github

undefined method `get' with Rspec

I have a problem with a very basic Rspec code, the same problem as the question 'undefined method `get' for #'.
But in my case none of the solutions given have worked for me!
I have my Rspec code at '/RailsProject/spec/controllers' and the code is:
require "../spec_helper"
describe "ApiMobile", :type => :controller do
it "Log In" do
get 'apiMobile/v0/logIn/test'
expect(response).to be_success
end
end
As you can see I've followed all the instructions but I still have the problem:
1) ApiMobile Log In
Failure/Error: get 'apiMobile/v0/logIn/test'
NoMethodError:
undefined method `get' for #<RSpec::Core::ExampleGroup::Nested_1:0x000000023b5ec8>
# ./api_mobile_controller_spec.rb:6:in `block (2 levels) in <top (required)>'
Finished in 0.00056 seconds
1 example, 1 failure
I have missed something or similar?
Thanks!
I finally discovered what was happening: Peter Alfvin was right, the fault was the isntallation.
I added at the Gemfile 'gem rspec' and at the command line typed 'rspec --init' after the 'bundle install'. But I also needed to add the gem 'rspec-rails' and type 'rails generate rspec:install'.
Now the get command works (I have another error but I think that is related to routes).

rails not allowing me to call a method from inside another method - rails keeps saying the method is undefined

In my model for tutoring sessions, I have code that triggers reminder texts at different times. Everything worked fine, until I tried some refactoring, and now I am having issues.
def send_reminder_text(texts_batch)
texts_batch.each do |text|
page_number = Refugee.find(text.refugee_id)[:last_page]
body_of_text = text[:begin_time].in_time_zone.strftime("Burma Reminder: upcoming session at %I:%M%p beginning on page #{page_number}.
Please email jek2141#columbia.edu to reschedule or cancel the session.")
text.begin_text(body_of_text)
end
end
def self.deliver_pm_reminder_text
texts_batch = TutoringSession.batch_for_pm_reminder_text
send_reminder_text(texts_batch)
end
def self.deliver_just_before_reminder_text
texts_batch = TutoringSession.batch_for_just_before_reminder_text
send_reminder_text(texts_batch)
end
When I call the deliver_just_before_reminder_text function, I get the following error message:
irb(main):006:0> TutoringSession.send_reminder_text
NoMethodError: undefined method `send_reminder_text' for #<Class:0x00000003cfe5d8>
from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record /base.rb:1088:in `method_missing'
from (irb):6
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.1.1/lib/rails/commands/console.rb:45:in `start'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.1.1/lib/rails/commands/console.rb:8:in `start'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.1.1/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
This is in spite of the fact that the send_reminder_text is clearly defined above.
Your method declaration specifies that the method is on objects of the TutoringSession class:
def send_reminder_text(texts_batch)
But you're trying to call it as if it were a method on the class itself:
irb(main):006:0> TutoringSession.send_reminder_text
Try changing your definition to:
def self.send_reminder_text(texts_batch)

Resources