Could anyone please explain what this test code does? :
assert_difference('Post.count') do
post :create, :post => { :title => 'Hi', :body => 'This is my first post.'}
end
and:
assert_difference 'ActionMailer::Base.deliveries.size', +1 do
post :invite_friend, :email => '[email protected]'
end
I can't understand it even though I read the documentation.
Thanks!
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
This is just checking to make sure that the number of objects for whatever type was specified has increased by 1. (It is an easy way to check to see that an object was added to the DB)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With