A classic finder returning nil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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