Rails初学者在这里..如何防止DELETE HTTP请求在这种情况下成功?
我有一个用户资源,我实现了一个应该阻止管理员用户删除自己的回调。
before_filter :admin_no_delete, only: :destroy
def admin_no_delete
admin_id = current_user.id if current_user.admin?
redirect_to root_path if params[:id] == admin_id
end
如果这看起来很熟悉了一些,从迈克尔·哈特尔的轨道教程,exercise #10 here的,但我试图以不同的方式去做,而不是他建议。
我的(跛脚)测试失败
describe "deleting herself should not be permitted" do
before do
delete user_path(admin)
end
it { should_not redirect_to(users_path) }
end
但暴露出删除链接管理员用户只是为了测试,并点击该链接,好像回调在执行实际成功(重定向到root_path )。
我能够使用jQuery删除记录调用销毁行动由回调(使用Web Inspector的JavaScript控制台)的保护:
$.ajax({url: 'http://localhost:3000/users/104', type: 'DELETE', success: function(result){alert(result)} })
如何防止一个DELETE HTTP请求寻找思路在这种情况下成功......还有关于如何正确测试这种情况的想法?
谢谢。
测试是否测试什么?你不成功删除后重定向吗? – 2012-04-04 11:33:09
实际上是否从数据库中删除了具有'id' 104的'User'? – 2012-04-04 11:33:12
@FrederickCheung - 谢谢,我意识到我的错误在那里...删除重定向到users_path,所以应该是一个should_not ..在这种情况下,测试实际上失败 – rme 2012-04-04 12:15:19