Understanding assert_difference in Ruby on Rails

后端 未结 4 1286
日久生厌
日久生厌 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:34

    This method is to verify that there is a numerical difference between first argument and the second argument. In Most cases first argument is a string which is like “Post.count” and second argument is a block. In rails this is mostly used in functional testing to check if an object can be saved in the database. Logic is that before a new is object being saved, number of records in that particular table must be different from the number of records after the object is saved ( from 1 to be precise).

提交回复
热议问题