开发者

Rails functional test - What is evaluated during assert_difference('SomeModel.count')?

开发者 https://www.devze.com 2023-02-01 12:32 出处:网络
In the functional test that is created by Rails (when generating a model using scaffolding), there is a test that looks like this:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消