Spec testing EventMachine-based (Reactor) Code
===

I'm trying out the whole `BDD` approach and would like to test the `AMQP`-based aspect of a vanilla `Ruby` application I am writing. After choosing `Minitest` as the test framework for its balance of features and expressiveness as opposed to other aptly-named vegetable frameworks, I set out to write this spec:

    # File ./test/specs/services/my_service_spec.rb

    # Requirements for test running and configuration
    require "minitest/autorun"
    require "./test/specs/spec_helper"

    # External requires
    # Minitest Specs for EventMachine
    require "em/minitest/spec"

    # Internal requirements
    require "./services/distribution/my_service"

    # Spec start
    describe "MyService", "A Gateway to an AMQP Server" do

      # Connectivity
      it "cannot connect to an unreachable AMQP Server" do

       # This line breaks execution, commented out
       # include EM::MiniTest::Spec

       # ...
       # (abridged) Alter the configuration by specifying
       # an invalid host such as "l0c@alho$t" or such
       # ...
 
       # Try to connect and expect to fail with an Exception
       MyApp::MyService.connect.must_raise EventMachine::ConnectionError
      end

    end

I have commented out the inclusion of [the em-minitest-spec gem][1]'s functionality which should coerce the spec to run inside the `EventMachine` reactor, if I include it I run into an even sketchier exception regarding (I suppose) inline classes and such: `NoMethodError: undefined method 'include' for #<#<Class:0x3a1d480>:0x3b29e00>`.

[1]:https://github.com/phiggins/em-minitest-spec

The code I am testing against, namely the `connect` method within that Service is based [on this article][2] and looks like this:

[2]:http://www.hiringthing.com/2011/11/04/eventmachine-with-rails.html

    # Main namespace
    module MyApp

      # Gateway to an AMQP Server
      class MyService

        # External requires
        require "eventmachine"
        require "amqp"

        # Main entry method, connects to the AMQP Server
        def self.connect

          # Add debugging, spawn a thread
          Thread.abort_on_exception = true
          begin
            @em_thread = Thread.new {
              begin
                EM.run do
                  @connection  = AMQP.connect(@settings["amqp-server"])
                  AMQP.channel = AMQP::Channel.new(@connection)
                end
              rescue
                raise
              end
            }

            # Fire up the thread
            @em_thread.join

            rescue Exception
              raise
            end
          end # method connect
      end
    end  # class MyService

The whole "_exception handling_" is merely an attempt to bubble the exception out to a place where I can catch/handle it, that didn't help either, with or without the `begin` and `raise` bits I still get the same result when running the spec:

`EventMachine::ConnectionError: unable to resolve server address`, which **actually is** what I would expect, yet `Minitest` doesn't play well with the whole reactor concept and fails the test on ground of this `Exception`.

The question then remains: How does one test `EventMachine`-related code using `Minitest`'s spec mechanisms? [Another question][3] has also been hovering around regarding `Cucumber`, also unanswered.

[3]:http://stackoverflow.com/questions/10592131/testing-eventmachine-with-cucumber

Or should I focus on my main functionality (e.g. messaging and seeing if the messages get sent/received) and forget about edge cases? Any insight would truly help!

Of course, it can all come down to the code I wrote above, maybe it's not the way one goes about writing/testing these aspects. Could be!

Notes on my environment: `ruby 1.9.3p194 (2012-04-20) [i386-mingw32]` (yes, Win32 :>), `minitest 3.2.0`, `eventmachine (1.0.0.rc.4 x86-mingw32)`, `amqp (0.9.7)`

Thanks in advance!