I'm wondering if there are better ways to write controller request specs than how I currently do it. I'm using the devise gem for authentication. This is how I'd test an admin controller:
describe "#index" do
context "when not logged in" do
it "redirects to root page" do
get admin_index_path
expect(response).to redirect_to root_path
end
end
context "when logged in as an user" do
before { sign_in user }
it "redirects to root page" do
get admin_index_path
expect(response).to redirect_to root_path
end
end
context "when logged in as an admin" do
before { sign_in admin }
it "opens the page" do
get admin_index_path
expect(response).to be_success
end
end
end
As you can see there is some "boilerplate" code which repeats over many of my controllers. For controllers which require a user to be logged in, I'd had to write a "not logged in" spec for every controller action. How do you do this? Is there a way to maybe shorten/share the code between the specs? The only thing that changes is the path.