Skip to content

Instantly share code, notes, and snippets.

@aaronjensen
Forked from RobinClowers/gist:2018620
Created March 12, 2012 03:41
Show Gist options
  • Save aaronjensen/2019573 to your computer and use it in GitHub Desktop.
Save aaronjensen/2019573 to your computer and use it in GitHub Desktop.
Rspec mock AAA extension
require 'rspec'
# is it really this simple?
RSpec::Matchers.define :have_received do |method_name|
match do |actual|
@args ||= []
@method_name ||= method_name
actual.received_message?(@method_name, *@args)
end
def with(*args)
@args = args
self
end
end
describe 'have received matcher' do
let(:foo) { stub.as_null_object }
it 'allows arrange, act, assert syntax' do
foo.go
foo.should have_received :go
end
it 'matches arguments' do
foo.go 'home'
foo.should have_received(:go).with('home')
end
it 'works with should_not' do
foo.should_not have_received :go
end
# fails
it 'works on already stubbed methods' do
foo.stub :go => true
foo.go
foo.should have_received :go
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment