I'm attempting to create some controller specs in my Rails 5 app using rspec, but the code keeps throwing the following error:
1) SessionsController Log in and log out logs in with valid user
Failure/Error: user = User.find_by(email: params[:session][:email].downcase)
NoMethodError:
undefined method `[]' for nil:NilClass
My spec is pretty straightforward. The user instance variable uses factory-girl to create a user with the email "user#example.com" and password as "password." When I call puts on these variables, I can see that they are set correctly.:
require 'rails_helper'
RSpec.describe SessionsController, type: :controller do
before :each do
#user = create(:user)
end
describe "Log in and log out" do
before :each do
post :create, { session: { email: #user.email,
password: #user.password }}
end
it "logs in with valid user" do
puts #user.email + " " + #user.password
expect(is_logged_in?).to be_truthy
end
end
end
Finally, the code from the sessions controller is below:
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
log_in user
remember user
redirect_to user
else
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
end
end
Am I misunderstanding the way params are sent to the controller in rspec? Is there any other reason for this error to be returned?
Changes were made in Rails 5 to the way you send params in controller tests.
Instead of:
before :each do
post :create, { session: { email: #user.email,
password: #user.password }}
end
You need to provide the params key in post request attribute hash. Like so...
before :each do
post :create, params: { session: { email: #user.email,
password: #user.password }}
end
It's subtle, but it's different. Let me know if that works.
Related
I'm going through Michael Hartl's Ruby tutorial and have been stuck for a day on a failing test
I get this when I run:
Error:
UsersControllerTest#test_should_redirect_edit_when_logged_in_as_wrong_user:
NoMethodError: undefined method `session' for nil:NilClass
test/test_helper.rb:19:in `log_in_as'
test/controllers/users_controller_test.rb:37:in `block in <class:UsersControllerTest>'
Here is the calling code:
require 'test_helper'
class UsersControllerTest < ActionDispatch::IntegrationTest
def setup
#user = users(:michael)
#otheruser = users(:archer)
end
test "should redirect update when logged in as wrong user" do
log_in_as(#other_user)
patch user_path(#user), params: { user: { name: #user.name,
email: #user.email } }
assert flash.empty?
assert_redirected_to root_url
end
*And here is the method I'm trying to call from the **test_helper** class:*
# Log in as a particular user
def log_in_as(user)
session[:user_id] = user.id
end
I was missing a part in my test_helper.rb class:
class ActionDispatch::IntegrationTest
# Log in as a particular user.
def log_in_as(user, password: 'password', remember_me: '1')
post login_path, params: { session: { email: user.email,
password: password,
remember_me: remember_me } }
end
end
Thank you for taking a look!
Did you include this line of code: include SessionsHelper in your application_controller.rb?
You have typo in setup method from the code above: #otheruser = users(:archer) should be #other_user = users(:archer)
Check again the code from the test file: test / controllers / users_controller_test.rb
especially this part of the code:
test "should redirect edit when logged in as wrong user" do
log_in_as(#other_user)
get edit_user_path(#user)
assert flash.empty?
assert_redirected_to root_url
end
Hope it helps!
I am learning to make tests for a ruby on rails application, and I appear to have run into an issue. I am trying to login a user as I do not have access to the session[:user_id] inside these two tests. So I have made a method inside the test_helper that defines these methods in my create_categories_test, and they run, but when I set a login for them, it returns this error:
(byebug) post login_path session: [{username: user.username, password: password}]
*** NoMethodError Exception: undefined method `[]' for nil:NilClass
This is my helper method in test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
def sign_in_as(user, password)
post login_path, session: {username: user.username, password: password}
end
end
Note: I put a debugger inside my method and ran the same line of code which returned nil(for some reason)
Here's my code for my create_categories_test.rb
def setup
#user = User.create(username: "John", email: "john#doe.com", password: "password", admin: true)
end
test "get new category form and create category" do
sign_in_as(#user, "password")
get new_category_path
assert_template 'categories/new'
assert_difference 'Category.count' do
post_via_redirect categories_path, category: {name: "sports"}
end
assert_template 'categories/index'
assert_match "sports", response.body
end
test "invalid category submission results in failure" do
sign_in_as(#user, "password")
get new_category_path
assert_template 'categories/new'
assert_no_difference 'Category.count', 1 do
post categories_path, category: {name: " "}
end
assert_template 'categories/new'
assert_select 'h2.panel-title'
assert_select 'div.panel-body'
end
My login controller:
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(username: params[:sessions][:username])
if user && user.authenticate(params[:sessions][:password])
session[:user_id] = user.id
flash[:success] = "You have successfully logged in"
redirect_to user_path(user)
else
flash.now[:danger] = "There was something wrong with your login details"
render 'new'
end
end
def destroy
session[:user_id] = nil
flash[:success] = "You have successfully logged out"
redirect_to root_path
end
end
I assume the problem in your params in the post method:
post login_path session: [{username: user.username, password: password}]
You post an array [{username: user.username, password: password}], but controller expect an hash:
post login_path session: {username: user.username, password: password}
Also your login helper:
#session: {}
post login_path, session: {username: user.username, password: password}
^^^^^^^
But controller expect:
#sessions: {}
user = User.find_by(username: params[:sessions][:username])
^^^^^^^^
Probably this is off-topic question, because it's about a simple typo in the code.
I'm coding a Rails 4 application to learn Rails & testing. My program code works as expected, but I can't figure out why I'm getting a no method error when posting to the create method in a Sessions controller test (RSpec v. 3.1.0) Here's the text of the error:
Failure/Error: post :create, email: "testerx#tester-x.net", password: "passwordx"
NoMethodError:
undefined method `[]' for nil:NilClass
This is relevant code from my Sessions Controller spec:
describe "POST create" do
context "with correct credentials" do
let!(:user) { User.create(user_name: "Testerx", email: "testerx#tester-x.net", password: "passwordx", password_confirmation: "passwordx", workout_enthusiast: "true" ) }
it "redirects to user show page" do
post :create, email: "testerx#tester-x.net", password: "passwordx"
expect(response).to be_redirect
expect(response).to redirect_to(users_show_path)
end
This is my Sessions Controller code:
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
# Logs the user in and redirects to the user's show page.
log_in user
params[:session][:remember_me] == '1' ? remember(user) : forget(user)
redirect_to user
else
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
log_out if logged_in?
redirect_to root_url
end
end
The error says undefined method for nil:NilClass. I'm sure the user is valid. I can't figure out why posting to the create method is not working in the test scenario. It works as expected in the application. What am I missing?
Change post :create, email: "testerx#tester-x.net", password: "passwordx" to post :create, session: { email: "testerx#tester-x.net", password: "passwordx" }.
The second argument of post is a parameter hash which will be sent to the controller. You are now passing { email: "testerx#tester-x.net", password: "passwordx" } to post, and obviously there is no session key in the parameter hash. When your controller tries to access paramas[:session][:xxx], it gets NoMethodError because params[:session] is nil, and nil does not have method [].
I have an app that uses Sorcery (or tries to use it),
and I am writing the specs for it:
context "successfull attempts to log in" do
let(:attr) { attributes_for(:credentials) }
before(:each) do
#user = create(:user, attr)
end
it "should log the user in" do
post :create, attr.merge(remember_me: false)
controller.should be_logged_in
end
end
Here is the FactoryGirl factory:
FactoryGirl.define do
factory :user do
email Faker::Internet.safe_email
password "password"
password_confirmation { |u| u.password }
client_id 1
end
factory :credentials, class: User do
email "user#example.com"
password "password"
end
end
And here is the controller action:
class SessionsController < ApplicationController
# ...
def create
login(params[:email], params[:email], params[:remember_me])
flash.now[:error] = "Invalid email/password combination"
render :new
end
end
The error message is the following:
1) SessionsController POST 'create' successfull attempts to log in should log the user in
Failure/Error: controller.should be_logged_in
expected logged_in? to return true, got false
# ./spec/controllers/sessions_controller_spec.rb:54:in `block (4 levels) in <top (required)>'
The spec keeps failing for some reason. Could anyone explain to me why please?
Your controller uses the email as both the username and password, which looks like a copy and paste error. Is that correct?
I have devise authentication and registration set up on my Rails app. I'm using after_sign_in_path_for() to customise the redirect when the user signs in based on various scenarios.
What I'm asking is how to test this method? It seems hard to isolate since it is called automatically by Devise when the user signes in. I want to do something like this:
describe ApplicationController do
describe "after_sign_in_path_for" do
before :each do
#user = Factory :user
#listing = Factory :listing
sign_in #user
end
describe "with listing_id on the session" do
before :each do
session[:listing_id] = #listing.id
end
describe "and a user in one team" do
it "should save the listing from the session" do
expect {
ApplicationController.new.after_sign_in_path_for(#user)
}.to change(ListingStore, :count).by(1)
end
it "should return the path to the users team page" do
ApplicationController.new.after_sign_in_path_for(#user).should eq team_path(#user.team)
end
end
end
end
end
but that's obviously not the way to do it because I just get an error:
Failure/Error: ApplicationController.new.after_sign_in_path_for(#user)
RuntimeError:
ActionController::Metal#session delegated to #_request.session, but #_request is nil: #<ApplicationController:0x00000104581c68 #_routes=nil, #_action_has_layout=true, #_view_context_class=nil, #_headers={"Content-Type"=>"text/html"}, #_status=200, #_request=nil, #_response=nil>
So, how can I test this method?
Oddly, I was wondering this very thing today. Here's what I came up with. I created an anonymous subclass of ApplicationController. In this anonymous subclass, I exposed the protected methods that I wanted to test as public methods. Then I tested them directly.
describe ApplicationController do
controller do
def after_sign_in_path_for(resource)
super resource
end
end
before (:each) do
#user = FactoryGirl.create(:user)
end
describe "After sigin-in" do
it "redirects to the /jobs page" do
controller.after_sign_in_path_for(#user).should == jobs_path
end
end
end
On a similar note - if you want to test the redirect after sign-up, you have two options.
First, you can follow a pattern similar to above and very directly test the method in RegistrationsController:
require 'spec_helper'
describe RegistrationsController do
controller(RegistrationsController) do
def after_sign_up_path_for(resource)
super resource
end
end
describe "After sign-up" do
it "redirects to the /organizations/new page" do
#user = FactoryGirl.build(:user)
controller.after_sign_up_path_for(#user).should == new_organization_path
end
end
end
Or, you can take a more integration-testing sort of approach and do the following:
require 'spec_helper'
describe RegistrationsController do
describe "After successfully completing the sign-up form" do
before do
#request.env["devise.mapping"] = Devise.mappings[:user]
end
it "redirects to the new organization page" do
post :create, :user => {"name" => "Test User", "email" => "test#example.com", "password" => "please"}
response.should redirect_to(new_organization_path)
end
end
end
For the newcomers, I would recommend doing this way:
RSpec.describe ApplicationController, type: :controller do
let(:user) { create :user }
describe "After sing-in" do
it "redirects to the /yourpath/ home page" do
expect(subject.after_sign_in_path_for(user)).to eq(yourpath_root_path)
end
end
end
I found this answer through Google recently and thought I would add my solution. I didn't like the accepted answer because it was testing the return value of a method on the application controller vs testing the desired behavior of the app.
I ended up just testing the call to create a new sessions as a request spec.
RSpec.describe "Sessions", type: :request do
it "redirects to the internal home page" do
user = FactoryBot.create(:user, password: 'password 123', password_confirmation: 'password 123')
post user_session_path, params: {user: {email: user.email, password: 'password 123'}}
expect(response).to redirect_to(internal_home_index_path)
end
end
(Rails 5, Devise 4, RSpec 3)
context "without previous page" do
before do
Factory.create(:user, email: "junior#example.com", password: "123456", password_confirmation: "123456")
request.env["devise.mapping"] = Devise.mappings[:user]
post :create, user: { email: "junior#example.com", password: "123456" }
end
end
it { response.should redirect_to(root_path) }
context "with previous page" do
before do
Factory.create(:user, email: "junior#example.com", password: "123456", password_confirmation: "123456")
request.env["devise.mapping"] = Devise.mappings[:user]
request.env['HTTP_REFERER'] = 'http://test.com/restaurants'
post :create, user: { email: "junior#example.com", password: "123456" }
end
it { response.should redirect_to("http://test.com/restaurants") }
end