1

我的控制器(用户控制器)

 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 ==)

怎么了?

4

1 回答 1

2

更正您的参数:

post :reset_password, :reset_code => @user.reset_code, :user => { :password => @attr[:password], :password_confirmation => @attr[:password_confirmation] }
于 2012-06-21T10:34:50.660 回答