2012-12-03 21 views
0

我正在关注密码重置时的Ryan Bates railscasts。我决定通过TDD实现他的代码,但我的一个测试拒绝工作。每当我跑Rspec不能与控制器中的<运算符一起工作

context "for checking update succeeds" do 
    before do 
    post :update, id: "2012-12-03 04:23:13 UTC" 
    end 

    it { should assign_to(:user) }  
    it { should respond_with(:redirect) } 
    it { should redirect_to(signin_path) } 
    it { should set_the_flash.to("Password has been reset.")} 

end 

我收到以下错误

Failure/Error: post :update, id: "2012-12-03 04:23:13 UTC" 
NoMethodError: 
    undefined method `<' for nil:NilClass 

我控制器如下

def update 
    @user = User.find_by_password_reset_token!(params[:id]) 
    if @user.password_reset_sent_at < 2.hours.ago 
    redirect_to new_password_reset_path, flash[:error] = "Password reset has expired" 
    elsif @user.update_attributes(params[:user]) 
    redirect_to root_url, flash[:success] = "Password has been reset." 
    else 
    render :edit 
    end 
end 

我必须假设我写我的测试错在某种形式或时尚。我该如何解决?请注意,我知道utc时间是截止日期,并基于昨天的时间。

+1

似乎'@ user.password_reset_sent_at'是'nil'。如果允许为'nil',则应通过if @ user.password_reset_sent_at && @ user.password_reset_sent_at <2.hours.ago'进行检查。如果它不应该被允许,你的模型中就有一个bug。测试看起来很好。 – Amadan

+0

谢谢,解决了这个问题。现在我需要去检查模型。发布这个作为答案,我会给你信用。 – jason328

回答

3

从评论复制:

好像@user.password_reset_sent_atnil。如果允许为nil,你应该

if @user.password_reset_sent_at && @user.password_reset_sent_at < 2.hours.ago 

检查如果它不应该被允许,你在你的模型中有一个错误的地方。测试看起来很好。

相关问题