Reputation: 927
so I'm writing a Test for my UserController, and the associated Devise dependency.
I'm trying to write a test that verify's userA can't access the show page of userB, but is redirected to the root_path instead. I'm guessing syntax errors are my issue, but I'd love another pair of eyes on it!
require 'rails_helper'
describe UsersController, :type => :controller do
# create test user
before do
@userA = User.create!(email: "[email protected]", password: "1234567890")
@userB = User.create!(email: "[email protected]", password: "1234567890")
end
describe "GET #show" do
before do
sign_in(@userA)
end
context "Loads correct user details" do
get :show
expect(response).to have_http_status(200)
expect(assigns(:user)).to eq @userA
end
context "No user is logged in" do
it "redirects to login" do
get :show, id: @userA.id
expect(response).to redirect_to(root_path)
end
end
end
describe "GET Unauthorized page" do
before do
sign_in(@userA)
end
context "Attempt to access show page of UserB" do
it "redirects to login" do
get :show, id: @userB.id
expect(response).to have_http_status(401)
expect(response).to redirect_to(root_path)
end
end
end
end
Upvotes: 0
Views: 312
Reputation: 1176
You're missing an "it" block in
context "Loads correct user details" do
get :show
expect(response).to have_http_status(200)
expect(assigns(:user)).to eq @userA
end
Upvotes: 1