【发布时间】:2015-09-20 06:25:30
【问题描述】:
在我的 rails 应用中,Signup Class 具有以下功能,
def register_email
# Something...
add_to_other_thread do
send_verification_email
end
end
def add_to_other_thread(&block)
Thread.new do
yield
ActiveRecord::Base.connection.close
end
end
我想用这些做 3 次测试。
- 关于 add_to_other_thread(&blcok) 的测试:
使用某个块调用 add_to_other_thread 后,检查它是否使用正确的块调用了 Thread.new。 - 关于 register_email 的测试:
调用 register_email 后,检查 add_to_other_thread(&block) 是否正确阻塞。 - 在积分测试中:
用户注册后,检查是否通过其他线程发送了正确的电子邮件(使用 ActionMailer::Base.deliveries)。
【问题讨论】:
-
@Drenmi 这个问题与“如何在 Rails 中测试 proc 和 yield?”不是重复的。上面的问题是关于“Thread.new”,后一个问题是关于“测试中的覆盖类或测试中的块延迟”。后一个问题仅以上述解决方案的可能方式与细节有关。
标签: ruby-on-rails multithreading unit-testing testing minitest