Skip to content

Instantly share code, notes, and snippets.

@kevinrutherford
Created July 6, 2012 14:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kevinrutherford/3060597 to your computer and use it in GitHub Desktop.
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