Skip to content

Instantly share code, notes, and snippets.

@sbellware
Created December 4, 2010 22:56
Show Gist options
  • Save sbellware/728575 to your computer and use it in GitHub Desktop.
Save sbellware/728575 to your computer and use it in GitHub Desktop.
Syntactic Sugar to Mimic Assert-Arrange-Act in RSpec 2 Without Test Spies
# sample class to mock and test against
class Thing
def foo
bar
end
def bar
end
end
# NOTE: Using the implicit subject in RSpec 2
describe Thing do
because { subject.foo }
it "calls foo" do
subject.should_have_received :bar
end
end
# this goes in spec_helper.rb (or your choice of alternative, eg: spec/support)
# just some sugar to rename 'after' to something more appropriate to the pattern
# as well as making the expectation matchers voice past tense
module RSpec
module Core
module Hooks
def because(*args, &block)
scope, options = scope_and_options_from(*args)
hooks[:after][scope] << AfterHook.new(options, &block)
end
end
end
module Mocks
module Methods
alias_method :should_have_received, :should_receive
alias_method :should_not_have_received, :should_not_receive
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment