我正在尝试编写Rspec测试以确保如果没有管理员登录,则用户无法创建邀请,然后重定向回sign_in页面
describe 'GET index', focus: true do
context 'with an admin user signed in' do
with :admin
let!(:invitation) { create :invitation }
before do
sign_in admin
get :index
end
it { should respond_with :ok }
it { should respond_with_content_type :html }
it { should render_template :index }
it 'assigns all invitations as @invitations' do
expect(assigns(:invitations)).to eq([invitation])
end
end
context 'with a non-admin user signed in' do
with :user
before do
sign_in user
get :index
end
it {should redirect_to new_user_session_path }
end
end
但重定向的东西不起作用。这是我遇到的失败:
1) InvitationsController GET index with a non-admin user signed in should redirect to "/users/sign_in"
Failure/Error: it {should redirect_to new_user_session_path }
Expected response to be a redirect to <http://test.host/users/sign_in> but was a redirect to <http://test.host/>.
Expected "http://test.host/users/sign_in" to be === "http://test.host/".
# ./spec/controllers/invitations_controller_spec.rb:26:in `block (4 levels) in <top (required)>'
我不确定如何解决它。我现在正在学习Rails,所以如果答案很明显我会道歉。感谢帮助!
答案 0 :(得分:1)
Devise的新用户登录页面检测到您的用户已登录,因此会将您的用户重定向到首页。
如果您真的希望用户在尝试访问邀请控制器而不是管理员时退出,那么您应该在失败的授权处理程序中强制注销。
答案 1 :(得分:0)
使用git reset --hard origin/dev-frank
git push origin dev-sam --force
进入rails控制台并输入rails c
。
在进行重定向时,您应该获得应用程序中所有路由的列表以及带有rake routes
后缀的前缀。
例如:
_path
如果此路线不存在,则必须创建该路线才能通过测试。