Testing

Tim Carey-Smith edited this page Oct 6, 2013 · 5 revisions

Dear @tarcieri, please to be expanding this content. Hugs, @mperham.

For now, there's this.

Test Mode

In your test/helper.rb or spec/spec_helper.rb, you should do this:

require 'celluloid/test'
require 'your-library'

This tells Celluloid to leave all the startup and shutdown to you, the developer.

We recommend restarting the system around each test/example.

The best way to do this is to use Celluloid.shutdown; Celluloid.boot before each spec.

You can always check the Celluloid spec_helper.rb for an example.

RSpec Notes

I've found that attempting to stub a method on an actor causes my RSpec tests to hang. For example:

class Foo
  include Celluloid
  def enabled?
    false
  end

  def do_work
    if enabled?
       do_stuff
    else
       do_other_stuff
    end
  end
end

describe Foo do
  describe "#do_work" do
    # this test hangs
    it "do stuff when enabled" do
      f = Foo.new
      f.stub(:enabled?) { true }
      f.should_receive(:do_stuff)
      f.do_work
    end

    # approach that's working for me (instance_eval on the #wrapped_object)
    it "do stuff when enabled" do
      f = Foo.new
      f.wrapped_object.instance_eval do
        def enabled?
          true
        end
      end
      f.should_receive(:do_stuff)
      f.do_work
    end
  end
end