~ubuntu-branches/ubuntu/oneiric/puppet/oneiric-security

« back to all changes in this revision

Viewing changes to vendor/gems/mocha-0.5.6/test/acceptance/standalone_acceptance_test.rb

  • Committer: Bazaar Package Importer
  • Author(s): Micah Anderson
  • Date: 2008-07-26 15:43:45 UTC
  • mto: (3.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20080726154345-1fmgo76b4l72ulvc
ImportĀ upstreamĀ versionĀ 0.24.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require File.join(File.dirname(__FILE__), "..", "test_helper")
 
2
require 'mocha_standalone'
 
3
 
 
4
class NotATestUnitAssertionFailedError < StandardError
 
5
end
 
6
 
 
7
class NotATestUnitTestCase
 
8
  
 
9
  include Mocha::Standalone
 
10
  
 
11
  attr_reader :assertion_count
 
12
  
 
13
  def initialize
 
14
    @assertion_count = 0
 
15
  end
 
16
  
 
17
  def run(test_method)
 
18
    mocha_setup
 
19
    begin
 
20
      prepare
 
21
      begin
 
22
        send(test_method)
 
23
        mocha_verify { @assertion_count += 1 }
 
24
      rescue Mocha::ExpectationError => e
 
25
        new_error = NotATestUnitAssertionFailedError.new(e.message)
 
26
        new_error.set_backtrace(e.backtrace)
 
27
        raise new_error
 
28
      ensure
 
29
        cleanup
 
30
      end
 
31
    ensure
 
32
      mocha_teardown
 
33
    end
 
34
  end
 
35
  
 
36
  def prepare
 
37
  end
 
38
  
 
39
  def cleanup
 
40
  end
 
41
    
 
42
end
 
43
 
 
44
class SampleTest < NotATestUnitTestCase
 
45
  
 
46
  def mocha_with_fulfilled_expectation
 
47
    mockee = mock()
 
48
    mockee.expects(:blah)
 
49
    mockee.blah
 
50
  end
 
51
  
 
52
  def mocha_with_unfulfilled_expectation
 
53
    mockee = mock()
 
54
    mockee.expects(:blah)
 
55
  end
 
56
  
 
57
  def mocha_with_unexpected_invocation
 
58
    mockee = mock()
 
59
    mockee.blah
 
60
  end
 
61
  
 
62
  def stubba_with_fulfilled_expectation
 
63
    stubbee = Class.new { define_method(:blah) {} }.new
 
64
    stubbee.expects(:blah)
 
65
    stubbee.blah
 
66
  end
 
67
  
 
68
  def stubba_with_unfulfilled_expectation
 
69
    stubbee = Class.new { define_method(:blah) {} }.new
 
70
    stubbee.expects(:blah)
 
71
  end
 
72
  
 
73
  def mocha_with_matching_parameter
 
74
    mockee = mock()
 
75
    mockee.expects(:blah).with(has_key(:wibble))
 
76
    mockee.blah(:wibble => 1)
 
77
  end
 
78
  
 
79
  def mocha_with_non_matching_parameter
 
80
    mockee = mock()
 
81
    mockee.expects(:blah).with(has_key(:wibble))
 
82
    mockee.blah(:wobble => 2)
 
83
  end
 
84
  
 
85
end
 
86
 
 
87
require 'test/unit'
 
88
 
 
89
class StandaloneAcceptanceTest < Test::Unit::TestCase
 
90
  
 
91
  attr_reader :sample_test
 
92
 
 
93
  def setup
 
94
    @sample_test = SampleTest.new
 
95
  end
 
96
  
 
97
  def test_should_pass_mocha_test
 
98
    assert_nothing_raised { sample_test.run(:mocha_with_fulfilled_expectation) }
 
99
    assert_equal 1, sample_test.assertion_count
 
100
  end
 
101
 
 
102
  def test_should_fail_mocha_test_due_to_unfulfilled_exception
 
103
    assert_raises(NotATestUnitAssertionFailedError) { sample_test.run(:mocha_with_unfulfilled_expectation) }
 
104
    assert_equal 1, sample_test.assertion_count
 
105
  end
 
106
 
 
107
  def test_should_fail_mocha_test_due_to_unexpected_invocation
 
108
    assert_raises(NotATestUnitAssertionFailedError) { sample_test.run(:mocha_with_unexpected_invocation) }
 
109
    assert_equal 0, sample_test.assertion_count
 
110
  end
 
111
 
 
112
  def test_should_pass_stubba_test
 
113
    assert_nothing_raised { sample_test.run(:stubba_with_fulfilled_expectation) }
 
114
    assert_equal 1, sample_test.assertion_count
 
115
  end
 
116
 
 
117
  def test_should_fail_stubba_test
 
118
    assert_raises(NotATestUnitAssertionFailedError) { sample_test.run(:stubba_with_unfulfilled_expectation) }
 
119
    assert_equal 1, sample_test.assertion_count
 
120
  end
 
121
 
 
122
  def test_should_pass_mocha_test_with_matching_parameter
 
123
    assert_nothing_raised { sample_test.run(:mocha_with_matching_parameter) }
 
124
    assert_equal 1, sample_test.assertion_count
 
125
  end
 
126
 
 
127
  def test_should_fail_mocha_test_with_non_matching_parameter
 
128
    assert_raises(NotATestUnitAssertionFailedError) { sample_test.run(:mocha_with_non_matching_parameter) }
 
129
  end
 
130
 
 
131
end
 
 
b'\\ No newline at end of file'