How can I check the code using "Thread.new" in Rails?

In my rails application, the registration class is below the functions,

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

And I want to do 3 tests with them.

  • Test about add_to_other_thread (& blcok):
    After add_to_other_thread is called with some block, check if it called Thread.new with the correct block.

  • Register_email test: After calling register_email, check if add_to_other_thread (& block) received the correct block or not.
  • In the integrated test:
    After registering the user, check if the proper e-mail was sent (using ActionMailer :: Base.deliveries) through another stream.
+6
2

Thread.new , Thready. Thread.new, Mock Thread, .

expect(Thread).to receive(:new).and_yield

ruby.

+1

@kwerle, add_to_other_thread Thread :

thread = signup.add_to_other_thread
thread.join
# your test goes here
0

Source: https://habr.com/ru/post/1608183/


All Articles