Understanding assert_difference in Ruby on Rails

后端 未结 4 1288
日久生厌
日久生厌 2021-02-13 02:43

Could anyone please explain what this test code does? :

assert_difference(\'Post.count\') do
    post :create, :post => { :title => \'Hi\', :body => \'T         


        
4条回答
  •  不思量自难忘°
    2021-02-13 03:40

    assert_difference verifies that the result of evaluating its first argument (a String which can be passed to eval) changes by a certain amount after calling the block it was passed. The first example above could be "unrolled" to:

    before = Post.count # technically, eval("Post.count")
    post :create, :post => { :title => 'Hi', :body => 'This is my first post.'}
    after = Post.count
    assert_equal after, before + 1
    

提交回复
热议问题