I've followed the recommendation from the Devise github pages for this:
Now this works great, but how would you test that you have this behavior now?
Well there are two ways of testing it one in the unit level by writing tests in the controllers that inherit the application controller. The code will look something like
it "should redirect to page_x after logged in" do
sign_in :user_role, @user
set_devise_mapping(:user_role)
get :new
response.should redirect_to(user_roles_dashboard_path)
end
For cucumber you should probably write a step to do the login and assert if u are on the expected after sign_in page.
If you are just using mini test, it would be something like this:
require 'test_helper'
class SessionsControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
test "admins should be redirected to comments_url" do
sign_in(users(:one))
post user_session_url
assert_redirected_to comments_url
end
test "No admins should be redirected to root_path" do
sign_in(users(:two))
post user_session_url
assert_redirected_to root_url
end
end