2012-06-21 77 views
1

我控制器(用戶控制器)RSpec的測試失敗顯示了Flash消息

def reset_password 
     @user = User.find_by_reset_code(params[:reset_code]) unless params[:reset_code].nil?  

     if request.post?  
     if @user && @user.update_attributes(:password => params[:user][:password], :password_confirmation => params[:user][:password_confirmation]) 
      self.current_user = @user 
      @user.delete_reset_code 
      flash[:success] = t('helpers.password_reset_successful') 
      render :template => "sessions/new" 
     else 
      flash[:error] = t('helpers.password_reset_error') 
      redirect_to root_path 
     end 
     end 
    end 

我想測試我做...

it "should have a flash message" do 
    @attr = { :password => "newpassword", :password_confirmation => "newpassword" } 
    @user.reset_code = Digest::SHA1.hexdigest(Time.now.to_s.split(//).sort_by {rand}.join) 

    post :reset_password, :reset_code => @user.reset_code, :password => @attr[:password], :password_confirmation => @attr[:password_confirmation] 
    flash[:success].should == I18n.t('helpers.password_reset_successful') 
    end 

但我在Rspec的已經例外 -

Failure/Error: flash[:success].should == I18n.t('helpers.password_reset_successful') 
     expected: "Password successfully updated" 
      got: nil (using ==) 

怎麼了?

回答

2

糾正你的PARAMS:

post :reset_password, :reset_code => @user.reset_code, :user => { :password => @attr[:password], :password_confirmation => @attr[:password_confirmation] } 
+0

不要工作。錯誤依然存在 – user1466717

+0

好的,保存你的用戶在發佈之前或使用相同的應該收到你的前一個問題 – apneadiving

+0

你能推薦一本關於RSpec的好書或網站嗎? – user1466717