-
-
Save aaronjensen/2019573 to your computer and use it in GitHub Desktop.
Rspec mock AAA extension
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
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