The best approach, I believe, is to drown out the Thread.new method and make sure that any βcomplexβ material has its own method that can be tested individually. So you will have something like this:
class Foo def start Thread.new do do_something end end def do_something loop do foo.bar(bar.foo) end end end
Then you check as follows:
describe Foo it "starts thread running do_something" do f = Foo.new expect(Thread).to receive(:new).and_yield expect(f).to receive(:do_something) f.start end it "do_something loops with and calls foo.bar with bar.foo" do f = Foo.new expect(f).to receive(:loop).and_yield
Thus, you do not need to go around so much to get the desired result.
source share