Reputation: 916
Im testing my Session Controller but Im getting this error, the log in feature works, I tested it on the browser but Im new to testing on Rspec and can't get this to work
Failure/Error: expect(response.session[:user_id]).to eq(@user_attr.id)
NoMethodError:
undefined method `session' for #<ActionController::TestResponse:0xd30df10>
# ./spec/controllers/sessions_controller_spec.rb:20:in `block (3 levels) in <top (required)>'
This is the code of my controller:
def new
@user = User.new
end
def create
@user = User.find_by(username: params[:user][:username])
if @user && @user.authenticate(params[:user][:password])
session[:user_id] = @user.id
redirect_to root_path
else
render :new
end
end
Rspec code:
require 'rails_helper'
RSpec.describe SessionsController, type: :controller do
describe "get Login page" do
it "returns http status" do
get :new
expect(response).to have_http_status(:success)
end
end
describe "session" do
before(:each) do
@user = FactoryGirl.create(:user)
@user_attr = FactoryGirl.attributes_for(:user)
end
it "gives session" do
request.session[:user_id] = nil
post :create, user: @user_attr
expect(response.session[:user_id]).to eq(@user_attr.id)
end
end
end
Upvotes: 3
Views: 2613
Reputation: 1854
session
is a variable that is available without the request/response context as shown in your example. If you want to manipulate it or check the values it contains, you can simply do something like this:
it "gives session" do
session[:user_id] = nil
post :create, user: @user_attr
expect(session[:user_id]).to eq(@user_attr.id)
end
Upvotes: 6