Ruby on rails 带和不带lambda+的控制器规格&引用;“应该改变”;
我正在测试一个简单的密码重置操作,并希望RSpec的“更改”匹配器用于lambdas。但它不适用于此控制器操作。没有火柴,一切都很好。以下是规格:Ruby on rails 带和不带lambda+的控制器规格&引用;“应该改变”;,ruby-on-rails,rspec,Ruby On Rails,Rspec,我正在测试一个简单的密码重置操作,并希望RSpec的“更改”匹配器用于lambdas。但它不适用于此控制器操作。没有火柴,一切都很好。以下是规格: describe "#update" do it "Updates the password and resets the token" do @user = Factory :user getter = lambda{ get :edit, :id => @user.perishable_token, :user =>
describe "#update" do
it "Updates the password and resets the token" do
@user = Factory :user
getter = lambda{
get :edit, :id => @user.perishable_token, :user => {:password_confirmation => "new_password",
:password => "new_password"}
@user.reload
}
getter.should change(@user, :password)
getter.should change(@user, :perishable_token)
end
it "Updates the password and resets the token" do
@user = Factory :user
old_password = @user.password
old_token = @user.perishable_token
get :edit, :id => @user.perishable_token, :user => {:password_confirmation => "new_password",
:password => "new_password"}
@user.reload.password.should != old_password
@user.perishable_token.should != old_token
end
end
第二个块起作用,第一个块不起作用。我试图打印lambda中的值,但它们确实没有改变
非常感谢您在这个问题上提出的任何想法 您需要使用
call
来实际执行第一个示例中的lambda。您将lambda分配给getter
,但从不执行getter.call
,以实际执行lambda并获得结果。因此,更改匹配器调用过程。所以这不是问题所在。但是,我调用的是编辑,而不是更新。所以什么都不应该改变。此外,由于未保存明文密码,密码仅存在于刚设置密码且未从数据库检索密码的用户对象上。以下是目前正在运行的代码:
describe "#update" do
it "Updates the password and resets the token" do
@user = Factory.create :user
getter = lambda{
post :update, :id => @user.perishable_token, :user => {:password_confirmation => "new_password",
:password => "new_password"}
@user.reload
}
getter.should change(@user, :crypted_password)
getter.should change(@user, :perishable_token)
end
end
我原以为这会是一个愚蠢的错误,但二合一…lambda似乎已经被执行了。我在lambda定义和期望之间尝试了“getter.call”,但没有任何改变。然后我尝试了“getter.call.should change(@user,:password)”,但出现了一个错误,说用户没有方法“call”。因此我得出结论,lamda返回的最后一条语句是@user.reload。这一点得到了证实,在执行的lamda末尾添加了一条print语句,错误消息更改为未定义的方法“call”,表示nil。