我正在尝试使用设计在我的应用程序上创建一个简单的测试。如果用户没有登录,我会设置重定向,但即使我在测试之前使用sign_in,我仍然会被重定向。当我手动测试它时,它工作正常。
george:
name: George
email: george@example.com
encrypted_password: <%= Devise::Encryptor.digest User, '123456' %>
georges_home:
title: Home
receiver: George
street: '132 Mitchell St SW'
country: 'US'
city: 'Atlanta'
postal_code: '30303'
phone: '+1 404-524-5665'
state: 'Georgia'
user: george
class AddressesController < ApplicationController
before_action :login_needed
def index
@addresses = Address.where(user_id: @current_user.id)
end
private
def login_needed
if !user_signed_in?
redirect_to new_user_session_path, alert: 'Please log in.'
end
end
end
require 'test_helper'
class AddressesControllerTest < ActionController::TestCase
include Devise::TestHelpers
setup do
sign_in users(:george)
@address = addresses(:georges_home)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:addresses)
end
end
答案 0 :(得分:0)
我找到了一段代码,用于处理具有设计和确认选项的用户模型的装置。所以我的用户&#39;夹具应如下所示:
george:
name: George
email: george@example.com
encrypted_password: <%= Devise::Encryptor.digest User, '123456' %>
confirmed_at: <%= Date.today %>