0

我最终会编写更精细的测试,但我什至无法开始使用自动生成的 rspec 测试进行设计。下面是代码,后面跟着错误。我添加的 puts 调用意味着事情正在按预期工作。为什么我什至不能显示应该已登录的用户?谢谢

describe UsersController do



 before (:each) do
    @user = FactoryGirl.create(:user)
    s = sign_in @user
    puts "signed in " + s.to_s
  end

  describe "GET 'show'" do

    it "should be successful" do
      puts "u #{@user.name}"
      get :show, :id => @user.id
      response.should be_success
    end

    it "should find the right user" do
      get :show, :id => @user.id
      assigns(:user).should == @user
    end

  end

失败:

  1) UsersController GET 'show' should find the right user
     Failure/Error: assigns(:user).should == @user
       expected: #<User id: 13, email: "example@example.com", encrypted_password: "$2a$04$oPxM0081ApmasEvX.wJ1dODL7.VsotWj06blfE8ve3B1...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2013-08-07 15:02:55", updated_at: "2013-08-07 15:02:55", name: "Test User", role: nil>
            got: nil (using ==)
     # ./spec/controllers/users_controller_spec.rb:21:in `block (3 levels) in <top (required)>'

  2) UsersController GET 'show' should be successful
     Failure/Error: response.should be_success
       expected success? to return true, got false
     # ./spec/controllers/users_controller_spec.rb:16:in `block (3 levels) in <top (required)>'

Finished in 0.09869 seconds
2 examples, 2 failures

这是我的用户控制器:

    class UsersController < ApplicationController
  before_filter :authenticate_user!

  def index
    authorize! :index, @user, :message => 'Not authorized as an administrator.'
    @users = User.all
  end

  def show
    @user = User.find(params[:id])
  end

  def update
    authorize! :update, @user, :message => 'Not authorized as an administrator.'
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user], :as => :admin)
      redirect_to users_path, :notice => "User updated."
    else
      redirect_to users_path, :alert => "Unable to update user."
    end
  end

  def destroy
    authorize! :destroy, @user, :message => 'Not authorized as an administrator.'
    user = User.find(params[:id])
    unless user == current_user
      user.destroy
      redirect_to users_path, :notice => "User deleted."
    else
      redirect_to users_path, :notice => "Can't delete yourself."
    end
  end
end
4

1 回答 1

0

尝试让用户在每个块的开头登录it "should ..." do,而不是在 :each 之前登录。我想我记得有一个类似的问题,如果我没记错的话,那就解决了。另外,我假设你有,因为 sign_in 正在工作,你是否包含了 Devise test_helpers,但如果没有,这也可能是问题的一部分。希望有帮助!

于 2013-08-09T05:42:14.263 回答