Simple BDD API for testing asynchronous Ruby/EventMachine code © 2008 Aman Gupta (tmm1)
em-spec can be used with either bacon or 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
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:
-
Git repository: github.com/tmm1/em-spec
-
Bacon: groups.google.com/group/comp.lang.ruby/browse_thread/thread/30b07b651b0662fd
-
Initial announcement: groups.google.com/group/eventmachine/browse_thread/thread/8b4e7ead72f9d013