这个想法是为了让管理员用户不会破坏自己。我写了以下测试:
describe "as admin user" do
let(:admin) { FactoryGirl.create(:admin) }
before { valid_signin admin }
describe "should not be able to delete himself by submitting a DELETE request to the Users#destroy action" do
specify do
expect { delete user_path(admin) }.not_to change(User, :count).by(-1)
end
end
end
并修改了销毁动作:
def destroy
@user = User.find(params[:id])
unless current_user?(@user)
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to users_url
end
end
(如果您是管理员用户,则只能访问销毁操作)。
测试现在应该通过,但它没有。我收到以下错误消息:
Failure/Error: expect { delete user_path(admin) }.not_to change(User, :count).by(-1)
ActionView::MissingTemplate:
Missing template users/destroy, application/destroy with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}.
我不明白缺少模板的错误消息,我不明白为什么测试没有通过。