Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A classic finder returning nil
Post = Class.new # Just a placeholder to make the tests run
class PublishesPosts < Struct.new(:post_id, :ui)
def run
post = Post.find_by_id(post_id)
if post
post.publish
ui.post_published(post)
else
ui.no_such_post(post_id)
end
end
end
describe PublishesPosts do
let(:post_id) { 137 }
let(:ui) { stub(:controller) }
subject { PublishesPosts.new(post_id, ui) }
after do
subject.run
end
context 'when the post exists' do
let(:post) { stub(:post) }
it 'publishes the post' do
Post.should_receive(:find).with(post_id).and_return(post)
post.should_receive(:publish)
ui.should_receive(:post_published).with(post)
end
end
context 'when the post cannot be found' do
it 'reports the error to the UI' do
Post.should_receive(:find).with(post_id).and_return(nil)
ui.should_receive(:no_such_post).with(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.