mocoso 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7de0f508008ca0672ee0c9b477cd56abcf6426dd
4
- data.tar.gz: 095191e5dd87d93e55776ff46694ba6424d9211d
3
+ metadata.gz: 76a80a460d924893db612381bac7a81d3ffaa1a2
4
+ data.tar.gz: 289580d3fea0301109093fc70445777dd4712d75
5
5
  SHA512:
6
- metadata.gz: 592564f4db16626077f2958b8dc52ba7d5289d8d0b7529239a0ddd8e27feb815c40ef623bc7c12e37009fc619b8717235109e88321fef82acedb9c71e1beef2b
7
- data.tar.gz: 32f6f94b0cd3cb383aceb4cd30b538ff532b6ed2a21e8cf4dd71d6ba702835c203bdd24abe589b7566013cf08f40212e9e8297d2dc77ebe4132175387d27ef89
6
+ metadata.gz: a965a66f88678883c46863c3d95f93887c476616e715853f1f903e41c3eff399b6060633bc657da45912b8702f3514946477ef29bc71732e814674f44ec20545
7
+ data.tar.gz: 1a82908dfd0d84c0119adc7f2eb3ec93b85e7f9cf6d8b05448325e493792beec2ab33f5f59f5f09095e96cfe754f9fc6c1286506fe5fd7cb11d58b4111d2ff2c
data/README.md CHANGED
@@ -47,7 +47,7 @@ A quick example (uses [Cutest][cutest]):
47
47
  test 'stubbing an instance method' do
48
48
  user = User.new
49
49
 
50
- stub user, valid?: true do
50
+ stub user, :valid?, true do
51
51
  assert user.valid?
52
52
  end
53
53
  end
@@ -78,35 +78,18 @@ module Mocoso
78
78
  # subject.bar('foo') # => "foo"
79
79
  # end
80
80
  #
81
- # If you try to stub a method that is not defined by the +object+,
82
- # it raises an error.
83
- #
84
- # Mocoso.stub Object.new, undefined: nil
85
- # # => NameError: undefined method `undefined' for class `Object'
86
- #
87
- # The same thing happens if a stubbed method is not invoked:
88
- #
89
- # Mocoso.stub subject, :foo, 'value' do
90
- # end
91
- # # => Expected method foo not invoked
92
- #
93
81
  def stub object, method, result, &block
94
82
  metaclass = object.singleton_class
95
83
  original = object.method method
96
- invoked = false
97
84
 
98
85
  metaclass.send :define_method, method do |*args|
99
- invoked = true
100
86
  result.respond_to?(:call) ? result.call(*args) : result
101
87
  end
102
88
 
103
- begin
104
- yield
105
- ensure
106
- metaclass.send :undef_method, method
107
- metaclass.send :define_method, method, original
108
- raise "Expected method #{method} not invoked" if !invoked
109
- end
89
+ yield
90
+ ensure
91
+ metaclass.send :undef_method, method
92
+ metaclass.send :define_method, method, original
110
93
  end
111
94
 
112
95
  # Expect that method +method+ is called with the arguments specified in the
@@ -129,22 +112,12 @@ module Mocoso
129
112
  # end
130
113
  #
131
114
  def expect object, method, options, &block
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
115
+ expectation = -> *params {
145
116
  with = options.fetch(:with) { [] }
146
117
  raise ExpectationError, "Expected #{with}, got #{params}" if params != with
147
- options.fetch :returns
148
- end
118
+ options.fetch(:returns)
119
+ }
120
+
121
+ stub object, method, expectation, &block
149
122
  end
150
123
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'mocoso'
5
- s.version = '1.0.1'
5
+ s.version = '1.1.0'
6
6
  s.summary = 'A simple stub & mock library'
7
7
  s.description = s.summary
8
8
  s.authors = ['Francesco Rodríguez']
@@ -18,7 +18,7 @@ setup do
18
18
  end
19
19
 
20
20
  test 'raises error if block is not given' do |subject|
21
- assert_raise { stub(subject, foo: 'foo') }
21
+ assert_raise { stub(subject, :foo, 'foo') }
22
22
  end
23
23
 
24
24
  test 'raises error if object not respond to the given method' do |subject|
@@ -80,19 +80,3 @@ test 'expectation with multiple arguments' do |subject|
80
80
  assert_equal 'new foo', subject.foo('new foo', optional: true)
81
81
  end
82
82
  end
83
-
84
- test 'raises error if stubbed method is never invoked' do |subject|
85
- assert_raise {
86
- stub subject, :foo, 'value' do
87
- end
88
- }
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: 1.0.1
4
+ version: 1.1.0
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-11 00:00:00.000000000 Z
11
+ date: 2013-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cutest