All Ruby tests raising: undefined method `authenticate' for nil:NilClass
This question has been answered on Twitter by @MatthewClosson
@jeffehh You need to create a
spec/support/devise.rb file as
specified here https://github.com/plataformatec/devise#test-helpers to
include the devise test helpers #ruby
Thanks once again.
Tests failing with undefined method `authenticate!' for nil:NilClass
You first need to add a call to the Devise minitest helpers in your test_helper.rb
class ActionController::TestCase
include Devise::Test::ControllerHelpers
end
Or if you have an older version of Devise:
class ActionController::TestCase
include Devise::TestHelpers
end
You then need to add a call to sign_in
before each test where a user should be signed in. In your example, change the setup to:
setup do
sign_in users(:one)
@location = locations(:one)
end
As @Iceman points out, this is documented in the Devise README.
Test not passing: undefined method `authenticate!' for nil:NilClass?
I had to add this:
spec_helpers.rb:
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
Rails 4.1 tests return undefined method `authenticate' for nil:NilClass
Found the bug it wasn't in the logic it was in the tests, I was testing the password with incorrect credentials treehouse123
instead of the one that was being created in my user teamtreehouse123
.
Inspiration came in this debugging code:
def create
user = User.find_by(email: params[:email])
if (user.nil? == false)
if user.authenticate(params[:password])
puts user.authenticate(params[:password])
puts "NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN Batman!"
session[:user_id] = user.id
flash[:success] = "Thanks for logging in!"
redirect_to todo_lists_path
else
puts user.authenticate(params[:password])
puts "waka waka waka waka waka waka waka waka waka waka waka waka waka waka waka waka"
flash[:error] = "There was an error logging in. Please check your email and password"
render action: "new"
end
else
flash[:error] = "There was an error logging in. Please check your email and password"
render action: "new"
end
end
user.authenticate(params[:password])
always returned false, regardless of what I did.
The Treehouse code is correct, but I'm changing all occurrences of teamtreehouse123
to password
.
undefined method `[]' for nil:NilClass + after login
The parameters you are sending are incorrect, you are taking the params value as a string, which is incorrect,
Try sending params as key and value,
Send Parameters as:
{
"utf8"=>"✓",
"authenticity_token"=>"b7t1yJ7cGyxDmecBpvBrGLRbh/ubtPjsUFdP/HosK8kp/ZLRtrV3dVWE2vPjDnThCbt9hk1ftm5ZMg9Ia1StWw==",
"email"=>{"pavanez4u@gmail.com",
"tabIndex"=>"1",
"password"=>"[FILTERED]"
"commit"=>"Log In"
}
@email = params[:email]
this line throws you an error because the params format is incorrect.
NoMethodError: undefined method 'customer_details' for nil:NilClass in RSpec
current_shop
is nil
in your controller. It's not enough that you set shop
in your spec code. Instance variables from specs are not shared with a controller under test.
Ensure that the shop you're creating in
@shop = create(:shop)
has field shopify_domain
set to whatever value has cookies[:shopify_domain]
in your test request.
Related Topics
Openssl VS Gpg for Encrypting Off-Site Backups
How to Run Shell Commands on Server in Capistrano V3
Hash['Key'] to Hash.Key in Ruby
How to Select Option in Drop Down Using Capybara
How to Use the Enumerable Mixin in My Class
Nicely Formatting Output to Console, Specifying Number of Tabs
Rails: Money Gem Converts All Amounts to Zero
Vagrant/Virtualbox Vm Provisioning: Rbenv Installs Successfully But Subsequent Uses in Script Fail
Where to Put Common Code Found in Multiple Models
Error Installing Debugger-Linecache in Ruby 1.9.3
Parse Command Line Arguments in a Ruby Script
If Java People Go to Scala, C# Go to F#, Where Do Ruby People Go for Functional Nirvana
"Uninitialized Constant" Error When Including a Module
Split Ruby Regex Over Multiple Lines