mock_zen 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +1 -1
- data/Rakefile +1 -1
- data/lib/mock/spy.rb +15 -0
- data/lib/mock/stub.rb +7 -3
- data/lib/mock/undefined_action_error.rb +1 -0
- data/lib/mock.rb +2 -5
- data/test/spy_test.rb +19 -10
- metadata +2 -2
data/README
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Mock zen is a
|
1
|
+
Mock zen is a mocking/stubbing gem that is currently under development. I developed my own library because I thought that none of the alternatives were really 'rubyist' or idiomatic. Mock zen is my humble attempt.
|
data/Rakefile
CHANGED
data/lib/mock/spy.rb
CHANGED
@@ -1,12 +1,27 @@
|
|
1
1
|
module Mock
|
2
2
|
|
3
|
+
# A Spy is a class that records every method call for further verification.
|
3
4
|
class Mock::Spy
|
4
5
|
|
6
|
+
# Creates the spy that will record any method calls on it.
|
7
|
+
# spy = Spy.new
|
8
|
+
# spy.called?(:method) # false
|
9
|
+
# spy.method
|
10
|
+
# spy.called?(:method) # true
|
5
11
|
def initialize
|
6
12
|
@methods_called = Hash.new(0)
|
7
13
|
end
|
8
14
|
|
15
|
+
# Returns true if the method has been called on the Spy.
|
16
|
+
# <code>method_name</code> must respond to to_sym.
|
9
17
|
def called?(method_name)
|
18
|
+
@methods_called[method_name.to_sym] > 0 if method_name.respond_to? :to_sym
|
19
|
+
end
|
20
|
+
|
21
|
+
# Returns the number of times that <code>method_name</code> was called
|
22
|
+
# on the spy object.
|
23
|
+
# <code>method_name</code> must respondo to to_sym.
|
24
|
+
def times_called(method_name)
|
10
25
|
@methods_called[method_name.to_sym] if method_name.respond_to? :to_sym
|
11
26
|
end
|
12
27
|
|
data/lib/mock/stub.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
module Mock
|
2
2
|
|
3
|
+
# A Stub is a class that will respond with the recorded action upon method calls.
|
3
4
|
class Mock::Stub
|
4
5
|
# Creates the stub with the actions passed. The stub will respond to the keys in
|
5
6
|
# the hash and return the corresponding values.
|
@@ -9,9 +10,12 @@ module Mock
|
|
9
10
|
@actions = actions
|
10
11
|
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
# Record a new behaviour to the stub as an alternative to passing all behaviours
|
14
|
+
# on the constructor.
|
15
|
+
# <br />
|
16
|
+
# Aliased as <code><<</code>
|
17
|
+
def add_behaviour(actions)
|
18
|
+
@actions.merge!(Mock::parse_actions(actions))
|
15
19
|
self
|
16
20
|
end
|
17
21
|
|
data/lib/mock.rb
CHANGED
@@ -9,6 +9,7 @@ module Mock
|
|
9
9
|
Stub.new(parse_actions(actions))
|
10
10
|
end
|
11
11
|
|
12
|
+
# Creates a Spy.
|
12
13
|
def Mock::Spy()
|
13
14
|
Spy.new
|
14
15
|
end
|
@@ -16,12 +17,8 @@ module Mock
|
|
16
17
|
protected
|
17
18
|
# Ensures that the actions have Symbol or String keys
|
18
19
|
def Mock::parse_actions(actions)
|
19
|
-
actions.reject! do |key, value|
|
20
|
-
!key.instance_of?(String) and !key.instance_of?(Symbol)
|
21
|
-
end
|
22
|
-
|
23
20
|
actions.each do |key, value|
|
24
|
-
if key.
|
21
|
+
if key.respond_to?(:to_sym)
|
25
22
|
actions.delete key
|
26
23
|
actions[key.to_sym] = value
|
27
24
|
end
|
data/test/spy_test.rb
CHANGED
@@ -3,19 +3,28 @@ require_relative 'test_helper'
|
|
3
3
|
class SpyTest < Test::Unit::TestCase
|
4
4
|
context "A spy" do
|
5
5
|
|
6
|
-
|
7
|
-
spy = Mock::Spy()
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
6
|
+
setup do
|
7
|
+
@spy = Mock::Spy()
|
8
|
+
end
|
9
|
+
|
10
|
+
should "record if a given method is called" do
|
11
|
+
@spy.method_call
|
12
|
+
assert @spy.called?(:method_call)
|
13
|
+
end
|
14
|
+
|
15
|
+
should "return false if the method wasn't called at all" do
|
16
|
+
assert not(@spy.called?(:method_call))
|
13
17
|
end
|
14
18
|
|
15
19
|
should "return 0 if the method wasn't called at all" do
|
16
|
-
|
17
|
-
|
18
|
-
|
20
|
+
assert_equal 0, @spy.times_called(:method_call)
|
21
|
+
end
|
22
|
+
|
23
|
+
should "return the number of times a method was called" do
|
24
|
+
5.times do
|
25
|
+
@spy.method_call
|
26
|
+
end
|
27
|
+
assert_equal 5, @spy.times_called(:method_call)
|
19
28
|
end
|
20
29
|
end
|
21
30
|
|