Skip to content
This repository
tree: 50e94e9b9b
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 162 lines (138 sloc) 4.79 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
require File.join(File.dirname(__FILE__), "acceptance_test_helper")
require 'bourne'
require 'matcher_helpers'

module SpyTestMethods

  def setup
    setup_acceptance_test
  end
  
  def teardown
    teardown_acceptance_test
  end
  
  def test_should_accept_wildcard_stub_call_without_arguments
    instance = new_instance
    instance.stubs(:magic)
    instance.magic
    assert_received(instance, :magic)
    assert_received(instance, :magic)
    assert_matcher_accepts have_received(:magic), instance
  end

  def test_should_accept_wildcard_stub_call_with_arguments
    instance = new_instance
    instance.stubs(:magic)
    instance.magic(:argument)
    assert_received(instance, :magic)
    assert_matcher_accepts have_received(:magic), instance
  end

  def test_should_not_accept_wildcard_stub_without_call
    instance = new_instance
    instance.stubs(:magic)
    assert_fails { assert_received(instance, :magic) }
    assert_fails { assert_matcher_accepts have_received(:magic), instance }
  end

  def test_should_not_accept_call_without_arguments
    instance = new_instance
    instance.stubs(:magic)
    instance.magic
    assert_fails { assert_received(instance, :magic) {|expect| expect.with(1) } }
    assert_fails { assert_matcher_accepts have_received(:magic).with(1), instance }
  end

  def test_should_not_accept_call_with_different_arguments
    instance = new_instance
    instance.stubs(:magic)
    instance.magic(2)
    assert_fails { assert_received(instance, :magic) {|expect| expect.with(1) } }
    assert_fails { assert_matcher_accepts have_received(:magic).with(1), instance }
  end

  def test_should_accept_call_with_correct_arguments
    instance = new_instance
    instance.stubs(:magic)
    instance.magic(1)
    assert_received(instance, :magic) {|expect| expect.with(1) }
    assert_matcher_accepts have_received(:magic).with(1), instance
  end

  def test_should_accept_call_with_wildcard_arguments
    instance = new_instance
    instance.stubs(:magic)
    instance.magic('hello')
    assert_received(instance, :magic) {|expect| expect.with(is_a(String)) }
    assert_matcher_accepts have_received(:magic).with(is_a(String)), instance
  end

  def test_should_reject_call_on_different_mock
    instance = new_instance
    other = new_instance
    instance.stubs(:magic)
    other.stubs(:magic)
    other.magic('hello')
    assert_fails { assert_received(instance, :magic) {|expect| expect.with(is_a(String)) } }
    assert_fails { assert_matcher_accepts have_received(:magic).with(is_a(String)), instance }
  end

  def test_should_accept_correct_number_of_calls
    instance = new_instance
    instance.stubs(:magic)
    2.times { instance.magic }
    assert_received(instance, :magic) {|expect| expect.twice }
    assert_matcher_accepts have_received(:magic).twice, instance
  end

  def test_should_not_allow_should_not
    begin
      have_received(:magic).does_not_match?(new_instance)
    rescue Mocha::API::InvalidHaveReceived => exception
      assert_match "should_not have_received(:magic) is invalid, please use should have_received(:magic).never", exception.message, "Test failed, but with the wrong message"
      return
    end
    flunk("Expected to fail")
  end

  def test_should_reject_not_enough_calls
    instance = new_instance
    instance.stubs(:magic)
    instance.magic
    message = /expected exactly twice/
    assert_fails(message) { assert_received(instance, :magic) {|expect| expect.twice } }
    assert_fails(message) { assert_matcher_accepts have_received(:magic).twice, instance }
  end

  def test_should_reject_too_many_calls
    instance = new_instance
    instance.stubs(:magic)
    2.times { instance.magic }
    message = /expected exactly once/
    assert_fails(message) { assert_received(instance, :magic) {|expect| expect.once } }
    assert_fails(message) { assert_matcher_accepts have_received(:magic).once, instance }
  end

  def assert_fails(message=/not yet invoked/)
    begin
      yield
    rescue FailedAssertion => exception
      assert_match message, exception.message, "Test failed, but with the wrong message"
      return
    end
    flunk("Expected to fail")
  end

end

class PartialSpyTest < Test::Unit::TestCase
  include AcceptanceTest
  include SpyTestMethods

  def new_instance
    Object.new
  end
end

class PureSpyTest < Test::Unit::TestCase
  include AcceptanceTest
  include SpyTestMethods

  def new_instance
    stub
  end
end

class StubEverythingSpyTest < Test::Unit::TestCase
  include AcceptanceTest
  def setup
    setup_acceptance_test
  end
  
  def teardown
    teardown_acceptance_test
  end
  def test_should_match_invocations_with_no_explicit_stubbing
    instance = stub_everything
    instance.surprise!
    assert_received(instance, :surprise!)
  end
end
Something went wrong with that request. Please try again.