Skip to content

Instantly share code, notes, and snippets.

@davybrion
Created September 15, 2012 13:27
Show Gist options
  • Save davybrion/3727873 to your computer and use it in GitHub Desktop.
Save davybrion/3727873 to your computer and use it in GitHub Desktop.
code snippet for "First Experiences With RSpec/BDD" post
class Publisher
include EventPublisher
event :my_first_event
event :my_second_event
def trigger_first_event(args)
trigger :my_first_event, args
end
def trigger_second_event(arg1, arg2)
trigger :my_second_event, arg1, arg2
end
end
describe EventPublisher, ": triggering event" do
before(:each) do
@publisher = Publisher.new
end
it "should not fail without any subscribers" do
@publisher.trigger_first_event "testing"
end
it "should pass single event arg correctly to subscribed method with one argument" do
@args = nil
def my_first_event_handler(args);
@args = args
end
@publisher.subscribe :my_first_event, method(:my_first_event_handler)
@publisher.trigger_first_event "testing!"
@args.should == "testing!"
end
it "should pass multiple event args correctly to subscribed method with multiple arguments" do
@args2_1, @args2_2 = nil, nil
def my_second_event_handler(arg1, arg2)
@args2_1, @args2_2 = arg1, arg2
end
@publisher.subscribe :my_second_event, method(:my_second_event_handler)
@publisher.trigger_second_event "second", "event"
@args2_1.should == "second"
@args2_2.should == "event"
end
it "should pass single event arg correctly to subscribed block with one argument" do
event_args = nil
@publisher.subscribe(:my_first_event) { |args| event_args = args }
@publisher.trigger_first_event "test"
event_args.should == "test"
end
it "should pass multiple event args correctly to subscribed block with two arguments" do
first_arg, second_arg = nil, nil
@publisher.subscribe(:my_second_event) { |arg1,arg2| first_arg, second_arg = arg1, arg2 }
@publisher.trigger_second_event "first", "second"
first_arg.should == "first"
second_arg.should == "second"
end
it "should call subscribed method once for each time it was subscribed" do
@counter1 = 0
def my_first_event_handler(args)
@counter1 += 1
end
2.times { @publisher.subscribe :my_first_event, method(:my_first_event_handler) }
@publisher.trigger_first_event "test"
@counter1.should == 2
end
it "should call all subscribed methods" do
@counter1, @counter2 = 0, 0
def handler1(args)
@counter1 += 1
end
def handler2(args)
@counter2 += 1
end
@publisher.subscribe :my_first_event, method(:handler1)
@publisher.subscribe :my_first_event, method(:handler2)
@publisher.trigger_first_event "first_event"
@counter1.should == 1
@counter2.should == 1
end
end
EventPublisher: triggering event
should not fail without any subscribers
should pass single event arg correctly to subscribed method with one argument
should pass multiple event args correctly to subscribed method with multiple arguments
should pass single event arg correctly to subscribed block with one argument
should pass multiple event args correctly to subscribed block with two arguments
should call subscribed method once for each time it was subscribed
should call all subscribed methods
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment