Tag: 测试设备

设计测试助手 – sign_in不起作用

出于某种原因,我无法使用设计辅助方法sign_in来工作。 current_user保持为null。 知道问题可能是什么? 测试: before :each do @user = FactoryGirl.create :user sign_in @user end describe “GET index” do it “assigns all subscribers as @subscribers” do subscriber = @user.subscribers.create! valid_attributes get :index assigns(:subscribers).should eq([subscriber]) end end 执行: def index @subscribers = current_user.subscribers.all <——- ERROR respond_to do |format| format.html # index.html.erb format.json { render json: @subscribers } […]

current_user使用Devise在控制器中返回TrueClass而不是User

我在Rails 2.3.8项目上使用Devise 1.0.8。 在我的控制器测试中,我有: context “when current_user is admin” do should “render” do sign_in Factory(:admin) get :index assert_response :success end end 在控制器本身,有以下before_filter: def redirect_if_not_admin puts current_user redirect_to root_path unless current_user.try(:admin?) end 这是我测试的输出: true false NoMethodError: undefined method `admin?’ for true:TrueClass 因此,当我在测试中使用sign_in时,current_user为true。 但是当我不这样做时,current_user是nil,所以肯定会受到影响。 在此先感谢您的帮助。