下面我列出了一些来自简单 Rails 应用程序的代码。下面列出的测试在最后一行失败,因为在此测试中 PostController 的更新操作中未更改帖子的updated_at字段。为什么?
这种行为在我看来有点奇怪,因为 Post 模型中包含标准时间戳,本地服务器上的实时测试表明该字段实际上是在从更新操作返回后更新的,并且第一个断言得到满足,因此它表明更新操作正常。
我怎样才能使固定装置在上述含义中可更新?
# 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