Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Simple BDD API for testing asynchronous Ruby/EventMachine code
Ruby
Branch: master
#1 Compare This branch is 17 commits ahead of tmm1:master.

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
README.rdoc
Rakefile
em-spec.gemspec

README.rdoc

Simple BDD API for testing asynchronous Ruby/EventMachine code © 2008 Aman Gupta (tmm1)

em-spec can be used with either bacon or rspec.

Rspec

There are two ways to use the Rspec extension. To use it as a helper, include EM::SpecHelper in your describe block. You then use the em method to wrap your evented test code. Inside the em block, you must call #done after your expectations. Everything works normally otherwise.

require "em-spec/rspec"
describe EventMachine do
  include EM::SpecHelper

  it "works normally when not using #em" do
    1.should == 1
  end

  it "makes testing evented code easy with #em" do
    em do
      start = Time.now

      EM.add_timer(0.5){
        (Time.now-start).should be_close( 0.5, 0.1 )
        done
      }
    end
  end
end

The other option is to include EM::Spec in your describe block. This will patch Rspec so that all of your examples run inside an em block automatically:

require "em-spec/rspec"
describe EventMachine do
  include EM::Spec

  it "requires a call to #done every time" do
    1.should == 1
    done
  end

  it "runs test code in an em block automatically" do
    start = Time.now

    EM.add_timer(0.5){
      (Time.now-start).should be_close( 0.5, 0.1 )
      done
    }
  end
end

Bacon

The API is identical to Bacon, except that you must explicitly call 'done' after all the current behavior's assertions have been made:

require 'em-spec/bacon'

EM.describe EventMachine do

  should 'have timers' do
    start = Time.now

    EM.add_timer(0.5){
      (Time.now-start).should.be.close 0.5, 0.1
      done
    }
  end

  should 'have periodic timers' do
    num = 0
    start = Time.now

    timer = EM.add_periodic_timer(0.5){
      if (num += 1) == 2
        (Time.now-start).should.be.close 1.0, 0.1
        EM.__send__ :cancel_timer, timer
        done
      end
    }
  end

end

Resources:

Something went wrong with that request. Please try again.