我正在尝试对我的控制器进行单元测试,使用 get
请求的每个测试都可以正常工作,但是在我使用其他调用(delete
在 destroy 中,post
在 create 中以及 put
在 update 中)的测试中会失败,并出现以下错误:
WARNING: Can't verify CSRF token authenticity
Completed 401 Unauthorized in 2.5ms
例如这是“destroy”功能的测试:
test "should destroy blog" do
assert_difference('Blog.count', -1) do
delete :destroy, id: @blog
end
assert_redirected_to blogs_path
end
无法工作
这是用于展示的测试,它可以正常工作:
test "should show blog" do
get :show, id: @blog
assert_response :success
end
在销毁(destroy)测试中,设备(devise)的
authenticate_user!
仅将我重定向到sign_in页面,测试失败。
test_helper.rb
中。请参见 https://stackoverflow.com/q/60940695/1196465 - David Gay