Skip to content

Instantly share code, notes, and snippets.

@steveklabnik
Created April 9, 2012 20:30
Show Gist options
  • Save steveklabnik/2346407 to your computer and use it in GitHub Desktop.
Save steveklabnik/2346407 to your computer and use it in GitHub Desktop.
require "minitest/autorun"
require "mocha"
require "watchable"
describe Watchable do
subject do
Object.new.extend Watchable
end
it "has an empty list of watchers by default" do
assert subject.watchers.empty?
end
it "returns an empty array of watchers for any event" do
assert_equal [], subject.watchers[:foo]
end
describe :fire do
it "calls each watcher with optional args" do
subject.on :foo, mock { expects(:call).with :bar, :baz }
subject.fire :foo, :bar, :baz
end
it "calls multiple watchers in order" do
fires = sequence "fires"
subject.on :foo, mock { expects(:call).in_sequence fires }
subject.on :foo, mock { expects(:call).in_sequence fires }
subject.fire :foo
end
it "returns the watchable" do
assert_same subject, subject.fire(:foo)
end
end
describe :off do
it "can unregister a block" do
b = lambda {}
subject.on :foo, &b
subject.off :foo, &b
assert subject.watchers[:foo].empty?
end
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment