Ruby on rails 如何使用';螺纹。新';在轨道上?
在我的rails应用程序中,注册类具有以下功能Ruby on rails 如何使用';螺纹。新';在轨道上?,ruby-on-rails,multithreading,unit-testing,testing,minitest,Ruby On Rails,Multithreading,Unit Testing,Testing,Minitest,在我的rails应用程序中,注册类具有以下功能 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个测试 关于
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个测试
在将_添加到_其他被某个块调用的_线程之后,检查它是否使用正确的块调用了thread.new
调用register\u电子邮件后,检查add\u to\u other\u线程(&block)是否得到了正确的阻止
用户注册后,检查是否通过其他线程发送了正确的电子邮件(使用ActionMailer::Base.deliveries)
创建Thread.new只需执行块,而不必执行繁琐的操作。stub Thread.new或Mock Thread或替换它
expect(Thread).to receive(:new).and_yield
另请参见除@kwerle answer外,您也可以在测试中为变量分配
add_to_other_thread
返回值,该值是thread
,然后等待该值完成后再运行测试:
thread = signup.add_to_other_thread
thread.join
# your test goes here
@Drenmi的可能重复此问题与“如何在rails中测试proc和yield?”不同。上面的问题是关于“Thread.new”,后面的问题是关于“在测试中重写类或在测试中阻塞延迟”。后一个问题是关于细节的,这只是上述解决方案的一种可能方式。