测试 assert_difference 干嘛用的?

xiaoronglv · October 14, 2012 · Last by pongyo replied at October 14, 2012 · 2902 hits

assert_difference(expressions, difference = 1, message = nil) {...}

Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.

没看懂文档,谁能给细说一下啊。附上一范例。

test "should create post" do
  assert_difference('Post.count') do
    post :create, :post => { :title => 'Hi', :body => 'This is my first post.'}
  end
  assert_redirected_to post_path(assigns(:post))
  assert_equal 'Post was successfully created.', flash[:notice]
end

非常感谢。

就是执行完 代码块里的内容后,Post.count 这个值会比执行前变化为 +1 (默认)

You need to Sign in before reply, if you don't have an account, please Sign up first.