Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using a listener
class PublishesPosts < Struct.new(:post_id, :ui)
def run
Blog.lookup_post(post_id, PublishesAPost.new(ui))
end
end
class PublishesAPost < Struct.new(:ui)
def post_found(post)
post.publish
ui.post_published(post)
end
def no_such_post(post_id)
ui.no_such_post(post_id)
end
end
describe PublishesPosts do
let(:post_id) { 137 }
let(:ui) { stub(:controller) }
let(:listener) { stub(:listener) }
subject { PublishesPosts.new(post_id, ui) }
describe '#run' do
PublishesAPost.should_receive(:new).with(ui).and_return(listener)
Blog.should_receive(:lookup_post).with(post_id, listener)
subject.run
end
end
describe PublishesAPost do
subject { PublishesAPost.new(ui) }
describe '#post_found' do
let(:post) { stub(:post) }
it 'publishes the post' do
post.should_receive(:publish)
ui.should_receive(:post_published).with(post)
subject.post_found(post)
end
end
describe '#no_such_post' do
it 'reports the error to the UI' do
post.should_not_receive(:publish)
ui.should_receive(:no_such_post).with(post_id)
subject.no_such_post(post_id)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.