File: standalone_test.rb

package info (click to toggle)
libmocha-ruby 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 944 kB
  • ctags: 1,384
  • sloc: ruby: 7,265; makefile: 4
file content (139 lines) | stat: -rw-r--r-- 3,393 bytes parent folder | download
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
require File.join(File.dirname(__FILE__), "acceptance_test_helper")
require 'mocha_standalone'
require 'simple_counter'

class NotATestUnitAssertionFailedError < StandardError
end

class NotATestUnitTestCase
  
  include Mocha::Standalone
  
  attr_reader :assertion_counter
  
  def initialize
    @assertion_counter = SimpleCounter.new
  end
  
  def run(test_method)
    mocha_setup
    begin
      prepare
      begin
        send(test_method)
        mocha_verify(@assertion_counter)
      rescue Mocha::ExpectationError => e
        new_error = NotATestUnitAssertionFailedError.new(e.message)
        new_error.set_backtrace(e.backtrace)
        raise new_error
      ensure
        cleanup
      end
    ensure
      mocha_teardown
    end
  end
  
  def prepare
  end
  
  def cleanup
  end
    
end

class SampleTest < NotATestUnitTestCase
  
  def mocha_with_fulfilled_expectation
    mockee = mock()
    mockee.expects(:blah)
    mockee.blah
  end
  
  def mocha_with_unfulfilled_expectation
    mockee = mock()
    mockee.expects(:blah)
  end
  
  def mocha_with_unexpected_invocation
    mockee = mock()
    mockee.blah
  end
  
  def stubba_with_fulfilled_expectation
    stubbee = Class.new { define_method(:blah) {} }.new
    stubbee.expects(:blah)
    stubbee.blah
  end
  
  def stubba_with_unfulfilled_expectation
    stubbee = Class.new { define_method(:blah) {} }.new
    stubbee.expects(:blah)
  end
  
  def mocha_with_matching_parameter
    mockee = mock()
    mockee.expects(:blah).with(has_key(:wibble))
    mockee.blah(:wibble => 1)
  end
  
  def mocha_with_non_matching_parameter
    mockee = mock()
    mockee.expects(:blah).with(has_key(:wibble))
    mockee.blah(:wobble => 2)
  end
  
end

require 'test/unit'

class StandaloneTest < Test::Unit::TestCase
  
  attr_reader :sample_test

  include AcceptanceTest
  
  def setup
    @sample_test = SampleTest.new
    setup_acceptance_test
  end
  
  def teardown
    teardown_acceptance_test
  end
  
  def test_should_pass_mocha_test
    assert_nothing_raised { sample_test.run(:mocha_with_fulfilled_expectation) }
    assert_equal 1, sample_test.assertion_counter.count
  end

  def test_should_fail_mocha_test_due_to_unfulfilled_exception
    assert_raises(NotATestUnitAssertionFailedError) { sample_test.run(:mocha_with_unfulfilled_expectation) }
    assert_equal 1, sample_test.assertion_counter.count
  end

  def test_should_fail_mocha_test_due_to_unexpected_invocation
    assert_raises(NotATestUnitAssertionFailedError) { sample_test.run(:mocha_with_unexpected_invocation) }
    assert_equal 0, sample_test.assertion_counter.count
  end

  def test_should_pass_stubba_test
    assert_nothing_raised { sample_test.run(:stubba_with_fulfilled_expectation) }
    assert_equal 1, sample_test.assertion_counter.count
  end

  def test_should_fail_stubba_test
    assert_raises(NotATestUnitAssertionFailedError) { sample_test.run(:stubba_with_unfulfilled_expectation) }
    assert_equal 1, sample_test.assertion_counter.count
  end

  def test_should_pass_mocha_test_with_matching_parameter
    assert_nothing_raised { sample_test.run(:mocha_with_matching_parameter) }
    assert_equal 1, sample_test.assertion_counter.count
  end

  def test_should_fail_mocha_test_with_non_matching_parameter
    assert_raises(NotATestUnitAssertionFailedError) { sample_test.run(:mocha_with_non_matching_parameter) }
  end

end