I am trying to write a rspec for sessions controller. But i am not able to do so.
I wanted to test controller for valid and invalid valid attributes. Tests should pass if valid attributes are passed for a particular user and should fail if invalid attributes are passed for a given user.
require 'rails_helper'
RSpec.describe SessionsController, type: :controller do
let(:user) { User.create(name: "some",email: '[email protected]', password: 'rous',password_confirmation: "some") }
describe "get login path" do
it "login page" do
get :new
expect(response).to render_template('sessions/new')
end
end
describe "valid attributes" do
it "create a session" do
post :create, session: { email: user.email, password: user.password }
expect(response.status).to render_template('/users/@user.id')
end
end
describe "invalid attributes" do
it "does not create a session" do
post :create, session: { email: user.email, password: "12345" }
expect(response.status).to render_template('sessions/new')
end
end
describe "does not create a session" do
it "if email is empty" do
post :create, session: {password: "some"}
expect(response.status).to render_template('sessions/new')
end
it "if password is empty" do
post :create, session: {email: "[email protected]"}
expect(response.status).to render_template('sessions/new')
end
end
end
Describe "invalid attributes" and "valid attributes" are not working fine.
If email/password are wrong, user redirected to session#new page.
If email & password are correct, user should to redirected to user_path i.e., users show page.