jimweirich / rspec-given

Given/When/Then keywords for RSpec Specifications
https://github.com/jimweirich/rspec-given
MIT License
653 stars 61 forks source link

Multiple Then's in one test breaks when testing Celluloid Actors #33

Closed cpuguy83 closed 11 years ago

cpuguy83 commented 11 years ago

In Celluloid it is recommended to shutdown and startup the Actor system before each test. before(:each) do Celluloid.shutdown Celluloid.boot end

When using multiple "Then"'s in one test, the Celluloid.shutdown has occurred as if it's a new test, but it doesn't look like my Given statements are being re-run as my actors are dead. Switching to "Then...And..." fixes this, but of course you don't get the test output when doing this.

jimweirich commented 11 years ago

Can you post a simple example of what you are doing, so I can see the situation in context? Thanks!

cpuguy83 commented 11 years ago
class Foo
  include Celluloid
end

before(:each) do
  Celluloid.shutdown
  Celluloid.boot
end

describe Foo do
  Given(:foo) { Foo.new }
  When { foo.something }
  Then { .. }
  Then { ... }
end

The 2nd "Then" statement fails because the actor has been shut down. Changing the 2nd "Then" to an "And" seems to work, but we don't get the test output from that, unless it fails.

jimweirich commented 11 years ago

I'm not getting an error. With this code:

require 'rspec/given'
require 'celluloid'

class Foo
  include Celluloid

  def something
    2
  end
end

describe Foo do
  before(:each) do
    Celluloid.shutdown
    Celluloid.boot
  end

  describe Foo do
    Given(:foo) { Foo.new }
    When(:result) { foo.something }
    Then { result == 2 }
    Then { result + 2 == 4 }
  end
end

I get:

$ rspec celluloid_spec.rb 
.D, [2013-10-03T09:25:37.477234 #65960] DEBUG -- : Terminating 5 actors...
.

Finished in 0.02838 seconds
2 examples, 0 failures
D, [2013-10-03T09:25:37.488354 #65960] DEBUG -- : Terminating 5 actors...
cpuguy83 commented 11 years ago

In: https://github.com/cpuguy83/monitoring_agent/blob/master/spec/agent_spec.rb#L8 If I change that from an And to a Then it fails with: 1) Agent.start Then { expect(result).to be_alive } Failure/Error: Then { expect(result).to be_alive } expected alive? to return true, got false

Works as an And.

Maybe make sure you aren't running in MRI?

jimweirich commented 11 years ago

You have a bug in your system. Agent returns a previously created (and dead) runner after you shutdown and reboot Celluloid. This test will demonstrate that:

  it "servives a reset" do
    Agent.start!
    runner  = Agent.runner
    expect(runner).to be_alive

    Celluloid.shutdown
    Celluloid.boot

    Agent.start!
    runner2 = Agent.runner
    expect(runner.object_id).to equal runner2.object_id
  end

Closing ... Not a problem in rspec-given.

cpuguy83 commented 11 years ago

Oh my gosh. Thanks Jim. I see it now. Sorry to waste your time there.