mocoso 0.1.2 → 1.0.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 035dcbfe0eaab282c1c61619e0fc79ba30cbdf79
4
- data.tar.gz: 80e2a9211e3259a74ac859aa42bded85b7fdf9f3
3
+ metadata.gz: 7de0f508008ca0672ee0c9b477cd56abcf6426dd
4
+ data.tar.gz: 095191e5dd87d93e55776ff46694ba6424d9211d
5
5
  SHA512:
6
- metadata.gz: f2a16d9f8df3110a1096a1de5ab7d5af509cad145ff98e53ba2945d5b2d4613efed33f06ce277ac952f06fb01657f20339c17dd62a6c82e0d6841ac61fc9f751
7
- data.tar.gz: 9ca6b09b6ac4e72432ae10b8a3a4ac40644f33704eb8ff52b2b21f6fc04fd133ce275fb2d50ec1a447b257d233d522471f30bdee21b868cfc16a4c0098e54c43
6
+ metadata.gz: 592564f4db16626077f2958b8dc52ba7d5289d8d0b7529239a0ddd8e27feb815c40ef623bc7c12e37009fc619b8717235109e88321fef82acedb9c71e1beef2b
7
+ data.tar.gz: 32f6f94b0cd3cb383aceb4cd30b538ff532b6ed2a21e8cf4dd71d6ba702835c203bdd24abe589b7566013cf08f40212e9e8297d2dc77ebe4132175387d27ef89
data/README.md CHANGED
@@ -4,6 +4,8 @@ Mocoso
4
4
  Yet Another Simple Stub & Mock library. This is stolen from tools such as
5
5
  [Minitest::Mock][minitest], [Override][override] and [Mocha][mocha].
6
6
 
7
+ **Made in Peru.**
8
+
7
9
  Motivation
8
10
  ----------
9
11
 
@@ -37,7 +39,7 @@ A quick example (uses [Cutest][cutest]):
37
39
  test 'mocking a class method' do
38
40
  user = User.new
39
41
 
40
- expect User, :find, with: [1], return: user do
42
+ expect User, :find, with: [1], returns: user do
41
43
  assert_equal user, User.find(1)
42
44
  end
43
45
  end
data/lib/mocoso.rb CHANGED
@@ -23,7 +23,7 @@
23
23
  # test 'mocking a class method' do
24
24
  # user = User.new
25
25
  #
26
- # expect User, :find, with: [1], return: user do
26
+ # expect User, :find, with: [1], returns: user do
27
27
  # assert_equal user, User.find(1)
28
28
  # end
29
29
  # end
@@ -70,7 +70,7 @@ module Mocoso
70
70
  #
71
71
  # You can pass a callable object (responds to +call+) as a value:
72
72
  #
73
- # Mocoso.stub subject, foo: -> { "foo" } do
73
+ # Mocoso.stub subject, :foo, -> { "foo" } do
74
74
  # subject.foo # => "foo"
75
75
  # end
76
76
  #
@@ -92,10 +92,8 @@ module Mocoso
92
92
  #
93
93
  def stub object, method, result, &block
94
94
  metaclass = object.singleton_class
95
- stubbed_method = "__mocoso_#{method}"
96
- invoked = false
97
-
98
- metaclass.send :alias_method, stubbed_method, method
95
+ original = object.method method
96
+ invoked = false
99
97
 
100
98
  metaclass.send :define_method, method do |*args|
101
99
  invoked = true
@@ -106,8 +104,7 @@ module Mocoso
106
104
  yield
107
105
  ensure
108
106
  metaclass.send :undef_method, method
109
- metaclass.send :alias_method, method, stubbed_method
110
- metaclass.send :undef_method, stubbed_method
107
+ metaclass.send :define_method, method, original
111
108
  raise "Expected method #{method} not invoked" if !invoked
112
109
  end
113
110
  end
@@ -123,7 +120,7 @@ module Mocoso
123
120
  #
124
121
  # user = User[1]
125
122
  #
126
- # Mocoso.expect user, :update, with: [{ name: 'new name' }], returns: true
123
+ # Mocoso.expect user, :update, with: [{ name: 'new name' }], returns: true do
127
124
  # subject.update unexpected: nil
128
125
  # # => Mocoso::ExpectationError: Expected [{:name=>"new name"}], got [{:unexpected=>nil}]
129
126
  #
@@ -132,12 +129,22 @@ module Mocoso
132
129
  # end
133
130
  #
134
131
  def expect object, method, options, &block
135
- expectation = -> *params {
132
+ stub object, method, Expectation.new(options), &block
133
+ end
134
+
135
+ # Intended for private use, but you can overwrite the +call+ method
136
+ # if you want to support more expectation options.
137
+ class Expectation # :nodoc:
138
+ attr :options
139
+
140
+ def initialize options
141
+ @options = options
142
+ end
143
+
144
+ def call *params
136
145
  with = options.fetch(:with) { [] }
137
146
  raise ExpectationError, "Expected #{with}, got #{params}" if params != with
138
- options.fetch(:returns)
139
- }
140
-
141
- stub object, method, expectation, &block
147
+ options.fetch :returns
148
+ end
142
149
  end
143
150
  end
data/mocoso.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'mocoso'
5
- s.version = '0.1.2'
5
+ s.version = '1.0.1'
6
6
  s.summary = 'A simple stub & mock library'
7
7
  s.description = s.summary
8
8
  s.authors = ['Francesco Rodríguez']
@@ -87,3 +87,12 @@ test 'raises error if stubbed method is never invoked' do |subject|
87
87
  end
88
88
  }
89
89
  end
90
+
91
+ test 'stub within a stub' do |subject|
92
+ stub subject, :foo, 'outer' do
93
+ stub subject, :foo, 'inner' do
94
+ assert_equal 'inner', subject.foo
95
+ end
96
+ assert_equal 'outer', subject.foo
97
+ end
98
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mocoso
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francesco Rodríguez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-10 00:00:00.000000000 Z
11
+ date: 2013-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cutest