2013-08-07 21 views
0

我最終會寫更詳細的測試,但我甚至無法開始自動生成rspec測試的設計。下面是代碼,然後是錯誤。我加入的看漲期權意味着事情按預期工作。爲什麼我甚至無法展示應該登錄的用戶?由於控制器testrspec與設計users_controller_spec

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: "[email protected]", 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 

這裏是我的users_controller:

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 
+1

你可以分享你的'UsersController'嗎? –

回答

0

嘗試讓用戶在每個it "should ..." do塊的開始登錄,而不是以前:每個。我想我記得有類似的問題,如果我沒有記錯的話,那就解決了。此外,我認爲你有,因爲sign_in工作,你有包括設計test_helpers,但如果沒有,這也可能是問題的一部分。希望有所幫助!