Project

em-rspec

0.01
No commit activity in last 3 years
No release in over 3 years
Test EventMachine code in RSpec 2
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 2.5.0

Runtime

>= 0.12.10
 Project Readme

em-rspec

em-rspec is a very simple patch to RSpec 2 that sets up and tears down an EventMachine reactor loop, and runs each example within the context of the loop. It also will wrap each example in a Fiber so you are free to untangle nested callbacks.

Usage

gem install em-rspec

require 'em-rspec'

em-rspec extends RSpec in an unobtrusive way that requires no addtional code in your specs to test EM code. e.g.

describe 'em-rspec' do
  it 'executes specs within a reactor loop' do
    EM.reactor_running?.should be_true # This is true
  end
end

How it works

As mentioned before, before the spec is run, a reactor loop is setup, and a callback to teardown the loop when your example finishes execution is created. This is an important point to consider because if you do any asynchronous i/o operations, your code will not be tested, because your code will likely have finished execution before the i/o operation is complete. It is an antipattern to actually hit the network in your tests and what you should do is mock out i/o calls to return immediately, e.g. http calls with webmock.

This is how your examples are run with em-rspec:

  • Set up reactor loop
  • Set callback to tear down reactor loop when spec is finished
  • Wrap example in fiber
  • Run before each block
  • Run example
  • Run after each block
  • Tear down reactor loop

© 2011 Stevie Graham