Skip to content

Instantly share code, notes, and snippets.

@aaronjensen
Forked from RobinClowers/gist:2018620
Created March 12, 2012 03:41
Show Gist options
  • Select an option

  • Save aaronjensen/2019573 to your computer and use it in GitHub Desktop.

Select an option

Save aaronjensen/2019573 to your computer and use it in GitHub Desktop.

Revisions

  1. aaronjensen revised this gist Mar 12, 2012. 2 changed files with 41 additions and 23 deletions.
    41 changes: 41 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    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
    23 changes: 0 additions & 23 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,23 +0,0 @@
    require 'rspec'

    # is it really this simple?
    RSpec::Matchers.define :have_received do |method_name, *args|
    match do |actual|
    actual.received_message?(method_name, *args, &block)
    end
    end

    describe 'have received matcher' do
    let(:foo) { double.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, 'home')
    end

    end
  2. @RobinClowers RobinClowers revised this gist Mar 12, 2012. 1 changed file with 8 additions and 3 deletions.
    11 changes: 8 additions & 3 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    require 'rspec'

    # is it really this simple?
    RSpec::Matchers.define :have_received do |expected|
    # is it really this simple?
    RSpec::Matchers.define :have_received do |method_name, *args|
    match do |actual|
    actual.received_message? expected
    actual.received_message?(method_name, *args, &block)
    end
    end

    @@ -15,4 +15,9 @@ describe 'have received matcher' do
    foo.should have_received :go
    end

    it 'matches arguments' do
    foo.go 'home'
    foo.should have_received(:go, 'home')
    end

    end
  3. @RobinClowers RobinClowers created this gist Mar 11, 2012.
    18 changes: 18 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    require 'rspec'

    # is it really this simple?
    RSpec::Matchers.define :have_received do |expected|
    match do |actual|
    actual.received_message? expected
    end
    end

    describe 'have received matcher' do
    let(:foo) { double.as_null_object }

    it 'allows arrange, act, assert syntax' do
    foo.go
    foo.should have_received :go
    end

    end