下面我列出了一些代码,从简单的Rails应用程序。下面列出的测试中最后一行失败,因为该职位的的updated_at场不PostController中的本次测试的更新操作中改变。为什么?如何在Rails的测试中更新灯具?
这种行为在我看来有些奇怪,因为标准时间戳包括在Post模型,本地服务器上现场测试表明,该领域是从更新的动作和第一个断言回国后实际更新从而满足它显示了更新操作就OK了。
我怎样才能使上面的意思灯具更新?
# app/controllers/post_controller.rb
def update
@post = Post.find(params[:id])
if @post.update_attributes(params[:post])
redirect_to @post # Update went ok!
else
render :action => "edit"
end
end
# test/functional/post_controller_test.rb
test "should update post" do
before = Time.now
put :update, :id => posts(:one).id, :post => { :content => "anothercontent" }
after = Time.now
assert_redirected_to post_path(posts(:one).id) # ok
assert posts(:one).updated_at.between?(before, after), "Not updated!?" # failed
end
# test/fixtures/posts.yml
one:
content: First post
非常感谢你,这是我一直在寻找的解决方案! – 2009-09-09 23:59:56