em-spec
em-spec copied to clipboard
Simple BDD API for testing asynchronous Ruby/EventMachine code
Simple BDD API for testing asynchronous Ruby/EventMachine code (c) 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:
- Git repository: http://github.com/tmm1/em-spec
- Bacon: http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/30b07b651b0662fd
- Initial announcement: http://groups.google.com/group/eventmachine/browse_thread/thread/8b4e7ead72f9d013