Rails functional test - What is evaluated during assert_difference('SomeModel.count')?
In the functional test that is created by Rails (when generating a model using scaffolding), there is a test that looks like this:
test "should create product" do
assert_difference('Product.count') do
pos开发者_JAVA百科t :create, ...
end
assert_redirected_to ...
end
My question is, what is actually evaluated by Product.count
?
Is it the number of rows in the products
table?
Is it the number of rows in the products table?
short answer - Yes
really it is running the ruby code Product.count, which just happens to execute the sql to get the count of all records in the products table.
I believe it runs the code before evaluating the block and then reruns it and compares the values after the block has executed
http://api.rubyonrails.org/classes/ActiveRecord/Calculations.html#method-i-count
http://api.rubyonrails.org/classes/ActiveSupport/Testing/Assertions.html#method-i-assert_difference
精彩评论