Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何使用';螺纹。新';在轨道上?_Ruby On Rails_Multithreading_Unit Testing_Testing_Minitest - Fatal编程技术网

Ruby on 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个测试 关于

在我的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个测试

  • 关于添加到其他线程(&blcok)的测试:
    在将_添加到_其他被某个块调用的_线程之后,检查它是否使用正确的块调用了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”,后面的问题是关于“在测试中重写类或在测试中阻塞延迟”。后一个问题是关于细节的,这只是上述解决方案的一种可能方式。