mock_zen 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -1 +1 @@
1
- Mock zen is a lightweight mocking system for Ruby.
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
@@ -2,7 +2,7 @@ desc "Erases the doc folder and recreates the RDoc"
2
2
  task :doc do
3
3
  doc_dir = File.join(File.dirname(__FILE__), "doc")
4
4
  FileUtils.rm_r(doc_dir) if File.exists?(doc_dir)
5
- `rdoc`
5
+ `rdoc -x test`
6
6
  end
7
7
 
8
8
  desc "Runs all tests"
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
- def add_behaviour(options)
13
- options = Mock::parse_actions(options)
14
- @actions.merge! options
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
 
@@ -1,5 +1,6 @@
1
1
  module Mock
2
2
 
3
+ # Error thrown when a method that wasn't recorded is called on a stub.
3
4
  class Mock::UndefinedActionError < RuntimeError
4
5
  end
5
6
 
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.instance_of? String
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
- should "record how many times a given method is called" do
7
- spy = Mock::Spy()
8
- 5.times do
9
- spy.method_call
10
- end
11
-
12
- assert_equal 5, spy.called?(:method_call)
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
- spy = Mock::Spy()
17
-
18
- assert_equal 0, spy.called?(:method_call)
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
 
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Guilherme Carvalho