理解 Ruby on Rails 中的 assert_difference [英] Understanding assert_difference in Ruby on Rails

查看:62
本文介绍了理解 Ruby on Rails 中的 assert_difference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下这个测试代码的作用?:

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

和:

assert_difference 'ActionMailer::Base.deliveries.size', +1 do
  post :invite_friend, :email => 'friend@example.com'
end

即使我阅读了文档,我也无法理解.

I can't understand it even though I read the documentation.

谢谢!

推荐答案

assert_difference 验证其第一个参数(可以传递给 eval 的字符串)的计算结果在调用它被传递的块.上面的第一个示例可以展开"为:

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

这篇关于理解 Ruby on Rails 中的 assert_difference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆