ryanprayogo
ryanprayogo

Reputation: 11817

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
      post :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?

Upvotes: 0

Views: 341

Answers (1)

house9
house9

Reputation: 20614

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

Upvotes: 1

Related Questions