我有一个测试,我试图通过,但我无法通过,我是新手。
这是我的git存储库https://github.com/FrankWiebe/nameofapp
的链接失败消息如下:
1) UsersController GET #show No user is logged in redirects to login
Failure/Error: expect(response).to redirect_to(root_path)
Expected response to be a <redirect>, but was <200>
# ./spec/controllers/users_controller_spec.rb:24:in `block (4 levels) in <top (required)>'
这是代码:
require 'rails_helper'
describe UsersController, :type => :controller do
before do
@user = User.create(email: "doe@example.com", password: "1234567890")
end
describe "GET #show" do
context "User is logged in" do
before do
sign_in @user
end
it "loads correct user details" do
get :show, id: @user.id
expect(response).to have_http_status(200)
expect(assigns(:user)).to eq @user
end
end
context "No user is logged in" do
it "redirects to login" do
get :show, id: @user.id
expect(response).to redirect_to(root_path)
end
end
end
end
答案 0 :(得分:0)
也许@user从一个测试登录到下一个测试,因此第二个节目成功。尝试在第一次测试时注销用户。
由于情况并非如此,请检查您的config / routes.rb。 需要登录的资源应该包含在do ... end
中authenticate(:user) do
resources :user
...other stuff
end