Skip to content

Instantly share code, notes, and snippets.

@workmaster2n
Created February 10, 2014 14:38
Show Gist options
  • Select an option

  • Save workmaster2n/8917058 to your computer and use it in GitHub Desktop.

Select an option

Save workmaster2n/8917058 to your computer and use it in GitHub Desktop.
Mock Spec
class CategoryCacheClearer < Struct.new(:article)
def call
article.category_list = ''
article.save
end
end
describe CategoryCacheClearer do
describe '#call' do
let(:article) { mock(:save, :category_list=) }
let(:category_cache_clearer) { CategoryCacheClearer.new(article) }
it 'clears the category list' do
article.should_receive(:category_list=)
category_cache_clearer.call
end
it 'calls save on the article' do
article.should_receive(:save)
category_cache_clearer.call
end
end
end
@patricksrobertson
Copy link
Copy Markdown

I tend to use bourne to spy on behavior like this so it'd be more like this:

it 'clears the category list' do
  category_cache_clearer.call

  expect(article).to have_received(:category_list=).with('')
end

It just reverses the steps so that you're keeping the standard four phase test (setup, execute, assert, teardown). You also want to ensure that you're testing the full interface on outgoing command messages, so ensuring that you're doing with('') is somewhat important.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment