mocha 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +3 -0
- data/README +148 -0
- data/lib/auto_mocha.rb +1 -0
- data/lib/auto_mocha/auto_mock.rb +54 -0
- data/lib/auto_mocha/mock_class.rb +38 -0
- data/lib/mocha.rb +1 -0
- data/lib/mocha/expectation.rb +116 -0
- data/lib/mocha/infinite_range.rb +27 -0
- data/lib/mocha/inspect.rb +37 -0
- data/lib/mocha/metaclass.rb +7 -0
- data/lib/mocha/mock.rb +30 -0
- data/lib/mocha/mock_methods.rb +55 -0
- data/lib/mocha/pretty_parameters.rb +28 -0
- data/lib/stubba.rb +2 -0
- data/lib/stubba/any_instance_method.rb +31 -0
- data/lib/stubba/class_method.rb +61 -0
- data/lib/stubba/instance_method.rb +22 -0
- data/lib/stubba/object.rb +77 -0
- data/lib/stubba/stubba.rb +27 -0
- data/lib/stubba/test_case.rb +65 -0
- data/test/all_tests.rb +100 -0
- data/test/auto_mocha/auto_mock_test.rb +85 -0
- data/test/auto_mocha/mock_class_test.rb +179 -0
- data/test/auto_mock_acceptance_test.rb +36 -0
- data/test/method_definer.rb +18 -0
- data/test/mocha/expectation_test.rb +216 -0
- data/test/mocha/infinite_range_test.rb +50 -0
- data/test/mocha/inspect_test.rb +79 -0
- data/test/mocha/mock_methods_test.rb +141 -0
- data/test/mocha/mock_test.rb +64 -0
- data/test/mocha/pretty_parameters_test.rb +32 -0
- data/test/mocha_acceptance_test.rb +112 -0
- data/test/stubba/any_instance_method_test.rb +113 -0
- data/test/stubba/class_method_test.rb +149 -0
- data/test/stubba/instance_method_test.rb +97 -0
- data/test/stubba/object_test.rb +147 -0
- data/test/stubba/stubba_test.rb +62 -0
- data/test/stubba/test_case_test.rb +41 -0
- data/test/stubba_acceptance_test.rb +107 -0
- data/test/stubba_integration_test.rb +59 -0
- data/test/stubba_replacer.rb +13 -0
- data/test/test_helper.rb +4 -0
- metadata +91 -0
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
2
|
+
require 'stubba/stubba'
|
3
|
+
require 'mocha/mock'
|
4
|
+
|
5
|
+
class StubbaTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
include Stubba
|
8
|
+
include Mocha
|
9
|
+
|
10
|
+
def test_should_start_with_empty_stubba_methods
|
11
|
+
stubba = Stubba.new
|
12
|
+
|
13
|
+
assert_equal [], stubba.stubba_methods
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_should_stub_method_if_not_already_stubbed
|
17
|
+
method = Mock.new
|
18
|
+
method.expects(:stub)
|
19
|
+
stubba = Stubba.new
|
20
|
+
|
21
|
+
stubba.stub(method)
|
22
|
+
|
23
|
+
method.verify
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_should_not_stub_method_if_already_stubbed
|
27
|
+
method = Mock.new
|
28
|
+
method.expects(:stub).times(0)
|
29
|
+
stubba = Stubba.new
|
30
|
+
stubba_methods = Mock.new
|
31
|
+
stubba_methods.stubs(:include?).with(method).returns(true)
|
32
|
+
stubba.stubba_methods = stubba_methods
|
33
|
+
|
34
|
+
stubba.stub(method)
|
35
|
+
|
36
|
+
method.verify
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_should_record_method
|
40
|
+
method = Mock.new
|
41
|
+
method.expects(:stub)
|
42
|
+
stubba = Stubba.new
|
43
|
+
|
44
|
+
stubba.stub(method)
|
45
|
+
|
46
|
+
assert_equal [method], stubba.stubba_methods
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_should_unstub_all_methods
|
50
|
+
stubba = Stubba.new
|
51
|
+
method_1 = Mock.new(:unstub => nil)
|
52
|
+
method_2 = Mock.new(:unstub => nil)
|
53
|
+
stubba.stubba_methods = [method_1, method_2]
|
54
|
+
|
55
|
+
stubba.unstub_all
|
56
|
+
|
57
|
+
assert_equal [], stubba.stubba_methods
|
58
|
+
method_1.verify
|
59
|
+
method_2.verify
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
2
|
+
require 'stubba_replacer'
|
3
|
+
require 'mocha/mock'
|
4
|
+
|
5
|
+
require 'stubba/test_case'
|
6
|
+
|
7
|
+
class TestCaseTest < Test::Unit::TestCase
|
8
|
+
|
9
|
+
include Mocha
|
10
|
+
|
11
|
+
def test_should_instantiate_new_stubba
|
12
|
+
test_class = Class.new(Test::Unit::TestCase) { def test_me; end }
|
13
|
+
test = test_class.new(:test_me)
|
14
|
+
replace_stubba(nil) do
|
15
|
+
test.setup
|
16
|
+
assert $stubba.is_a?(Stubba::Stubba)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_should_unstub_all_stubbed_methods
|
21
|
+
test_class = Class.new(Test::Unit::TestCase) { def test_me; end }
|
22
|
+
test = test_class.new(:test_me)
|
23
|
+
stubba = Mock.new(:unstub_all => nil)
|
24
|
+
replace_stubba(stubba) do
|
25
|
+
test.teardown
|
26
|
+
end
|
27
|
+
stubba.verify(:unstub_all)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_should_set_stubba_to_nil
|
31
|
+
test_class = Class.new(Test::Unit::TestCase) { def test_me; end }
|
32
|
+
test = test_class.new(:test_me)
|
33
|
+
stubba = Mock.new(:unstub_all => nil)
|
34
|
+
replace_stubba(stubba) do
|
35
|
+
test.teardown
|
36
|
+
assert_nil $stubba
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'stubba'
|
3
|
+
|
4
|
+
class Widget
|
5
|
+
|
6
|
+
def model
|
7
|
+
'original_model'
|
8
|
+
end
|
9
|
+
|
10
|
+
class << self
|
11
|
+
|
12
|
+
def find(options)
|
13
|
+
[]
|
14
|
+
end
|
15
|
+
|
16
|
+
def create(attributes)
|
17
|
+
Widget.new
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
module Thingy
|
25
|
+
|
26
|
+
def self.wotsit
|
27
|
+
:hoojamaflip
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
class StubbaAcceptanceTest < Test::Unit::TestCase
|
33
|
+
|
34
|
+
def test_should_stub_instance_method
|
35
|
+
widget = Widget.new
|
36
|
+
widget.expects(:model).returns('different_model')
|
37
|
+
assert_equal 'different_model', widget.model
|
38
|
+
widget.verify
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_should_stub_module_method
|
42
|
+
should_stub_module_method
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_should_stub_module_method_again
|
46
|
+
should_stub_module_method
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_should_stub_class_method
|
50
|
+
should_stub_class_method
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_should_stub_class_method_again
|
54
|
+
should_stub_class_method
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_should_stub_instance_method_on_any_instance_of_a_class
|
58
|
+
should_stub_instance_method_on_any_instance_of_a_class
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_should_stub_instance_method_on_any_instance_of_a_class_again
|
62
|
+
should_stub_instance_method_on_any_instance_of_a_class
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_should_stub_two_different_class_methods
|
66
|
+
should_stub_two_different_class_methods
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_should_stub_two_different_class_methods_again
|
70
|
+
should_stub_two_different_class_methods
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def should_stub_module_method
|
76
|
+
Thingy.expects(:wotsit).returns(:dooda)
|
77
|
+
assert_equal :dooda, Thingy.wotsit
|
78
|
+
Thingy.verify
|
79
|
+
end
|
80
|
+
|
81
|
+
def should_stub_class_method
|
82
|
+
widgets = [Widget.new]
|
83
|
+
Widget.expects(:find).with(:all).returns(widgets)
|
84
|
+
assert_equal widgets, Widget.find(:all)
|
85
|
+
Widget.verify
|
86
|
+
end
|
87
|
+
|
88
|
+
def should_stub_two_different_class_methods
|
89
|
+
found_widgets = [Widget.new]
|
90
|
+
created_widget = Widget.new
|
91
|
+
Widget.expects(:find).with(:all).returns(found_widgets)
|
92
|
+
Widget.expects(:create).with(:model => 'wombat').returns(created_widget)
|
93
|
+
assert_equal found_widgets, Widget.find(:all)
|
94
|
+
assert_equal created_widget, Widget.create(:model => 'wombat')
|
95
|
+
Widget.verify
|
96
|
+
end
|
97
|
+
|
98
|
+
def should_stub_instance_method_on_any_instance_of_a_class
|
99
|
+
Widget.any_instance.expects(:model).at_least_once.returns('another_model')
|
100
|
+
widget_1 = Widget.new
|
101
|
+
widget_2 = Widget.new
|
102
|
+
assert_equal 'another_model', widget_1.model
|
103
|
+
assert_equal 'another_model', widget_2.model
|
104
|
+
Widget.any_instance.verify
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'stubba_replacer'
|
3
|
+
|
4
|
+
require 'stubba/object'
|
5
|
+
require 'stubba/test_case'
|
6
|
+
|
7
|
+
class StubbaIntegrationTest < Test::Unit::TestCase
|
8
|
+
|
9
|
+
class DontMessWithMe
|
10
|
+
def self.method_x
|
11
|
+
:original_return_value
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_should_stub_class_method_within_test
|
16
|
+
test_class = Class.new(Test::Unit::TestCase) do
|
17
|
+
def test_me
|
18
|
+
DontMessWithMe.expects(:method_x).returns(:new_return_value)
|
19
|
+
assert_equal :new_return_value, DontMessWithMe.method_x
|
20
|
+
end
|
21
|
+
end
|
22
|
+
test = test_class.new(:test_me)
|
23
|
+
|
24
|
+
replace_stubba(nil) do
|
25
|
+
test_result = Test::Unit::TestResult.new
|
26
|
+
test.run(test_result) {}
|
27
|
+
assert test_result.passed?
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_should_leave_stubbed_class_unchanged_after_test
|
32
|
+
test_class = Class.new(Test::Unit::TestCase) do
|
33
|
+
def test_me
|
34
|
+
DontMessWithMe.expects(:method_x).returns(:new_return_value)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
test = test_class.new(:test_me)
|
38
|
+
|
39
|
+
replace_stubba(nil) do
|
40
|
+
test.run(Test::Unit::TestResult.new) {}
|
41
|
+
assert_equal :original_return_value, DontMessWithMe.method_x
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_should_reset_expectations_after_test
|
46
|
+
test_class = Class.new(Test::Unit::TestCase) do
|
47
|
+
def test_me
|
48
|
+
DontMessWithMe.expects(:method_x)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
test = test_class.new(:test_me)
|
52
|
+
|
53
|
+
replace_stubba(nil) do
|
54
|
+
test.run(Test::Unit::TestResult.new) {}
|
55
|
+
end
|
56
|
+
assert_equal 0, DontMessWithMe.mocha.expectations.size
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.11
|
3
|
+
specification_version: 1
|
4
|
+
name: mocha
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: "0.1"
|
7
|
+
date: 2006-07-16 00:00:00 +01:00
|
8
|
+
summary: Mocking and stubbing library
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: mocha-developer@rubyforge.org
|
12
|
+
homepage: http://mocha.rubyforge.org
|
13
|
+
rubyforge_project: mocha
|
14
|
+
description: "Mocking and stubbing library with JMock/SchMock syntax, which allows mocking and
|
15
|
+
stubbing of methods on real (non-mock) classes. Includes auto-mocking which
|
16
|
+
magically provides mocks for undefined classes, facilitating unit tests with no
|
17
|
+
external dependencies."
|
18
|
+
autorequire: mocha
|
19
|
+
default_executable:
|
20
|
+
bindir: bin
|
21
|
+
has_rdoc: true
|
22
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
23
|
+
requirements:
|
24
|
+
-
|
25
|
+
- ">"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 0.0.0
|
28
|
+
version:
|
29
|
+
platform: ruby
|
30
|
+
signing_key:
|
31
|
+
cert_chain:
|
32
|
+
authors:
|
33
|
+
- James Mead
|
34
|
+
files:
|
35
|
+
- lib/auto_mocha.rb
|
36
|
+
- lib/mocha.rb
|
37
|
+
- lib/stubba.rb
|
38
|
+
- lib/auto_mocha/auto_mock.rb
|
39
|
+
- lib/auto_mocha/mock_class.rb
|
40
|
+
- lib/mocha/expectation.rb
|
41
|
+
- lib/mocha/infinite_range.rb
|
42
|
+
- lib/mocha/inspect.rb
|
43
|
+
- lib/mocha/metaclass.rb
|
44
|
+
- lib/mocha/mock.rb
|
45
|
+
- lib/mocha/mock_methods.rb
|
46
|
+
- lib/mocha/pretty_parameters.rb
|
47
|
+
- lib/stubba/any_instance_method.rb
|
48
|
+
- lib/stubba/class_method.rb
|
49
|
+
- lib/stubba/instance_method.rb
|
50
|
+
- lib/stubba/object.rb
|
51
|
+
- lib/stubba/stubba.rb
|
52
|
+
- lib/stubba/test_case.rb
|
53
|
+
- test/all_tests.rb
|
54
|
+
- test/auto_mock_acceptance_test.rb
|
55
|
+
- test/method_definer.rb
|
56
|
+
- test/mocha_acceptance_test.rb
|
57
|
+
- test/stubba_acceptance_test.rb
|
58
|
+
- test/stubba_integration_test.rb
|
59
|
+
- test/stubba_replacer.rb
|
60
|
+
- test/test_helper.rb
|
61
|
+
- test/auto_mocha/auto_mock_test.rb
|
62
|
+
- test/auto_mocha/mock_class_test.rb
|
63
|
+
- test/mocha/expectation_test.rb
|
64
|
+
- test/mocha/infinite_range_test.rb
|
65
|
+
- test/mocha/inspect_test.rb
|
66
|
+
- test/mocha/mock_methods_test.rb
|
67
|
+
- test/mocha/mock_test.rb
|
68
|
+
- test/mocha/pretty_parameters_test.rb
|
69
|
+
- test/stubba/any_instance_method_test.rb
|
70
|
+
- test/stubba/class_method_test.rb
|
71
|
+
- test/stubba/instance_method_test.rb
|
72
|
+
- test/stubba/object_test.rb
|
73
|
+
- test/stubba/stubba_test.rb
|
74
|
+
- test/stubba/test_case_test.rb
|
75
|
+
- README
|
76
|
+
- COPYING
|
77
|
+
test_files:
|
78
|
+
- test/all_tests.rb
|
79
|
+
rdoc_options:
|
80
|
+
- "--title"
|
81
|
+
- Mocha
|
82
|
+
- "--main"
|
83
|
+
- README
|
84
|
+
- "--line-numbers"
|
85
|
+
extra_rdoc_files:
|
86
|
+
- README
|
87
|
+
- COPYING
|
88
|
+
executables: []
|
89
|
+
extensions: []
|
90
|
+
requirements: []
|
91
|
+
dependencies: []
|