soveran-override 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +5 -4
- data/lib/override.rb +14 -12
- data/test/all_test.rb +2 -0
- metadata +1 -1
data/README.markdown
CHANGED
@@ -6,7 +6,7 @@ The as-simple-as-possible-but-not-simpler stubbing library.
|
|
6
6
|
Description
|
7
7
|
-----------
|
8
8
|
|
9
|
-
|
9
|
+
Override is the essence of the stubbing concept: it takes an object,
|
10
10
|
a hash of methods/results, and proceeds to rewrite each method in the
|
11
11
|
object. It can be used as a stubbing strategy in most cases, and I'd
|
12
12
|
say that cases that don't fit this pattern have a very bad code smell,
|
@@ -17,8 +17,10 @@ Usage
|
|
17
17
|
|
18
18
|
require 'override'
|
19
19
|
|
20
|
+
include Override
|
21
|
+
|
20
22
|
user = User.spawn
|
21
|
-
override(
|
23
|
+
override(user, :name => "Foobar", :email => "foobar@example.org")
|
22
24
|
override(User, :find => user)
|
23
25
|
|
24
26
|
Or alternatively:
|
@@ -42,8 +44,7 @@ And then, in your tests:
|
|
42
44
|
|
43
45
|
assert_equal "Foobar", User.find(1).name
|
44
46
|
|
45
|
-
In case you don't know what spawn means, check my
|
46
|
-
testing at http://github.com/soveran/spawner.
|
47
|
+
In case you don't know what spawn means, check my library [Spawner](http://github.com/soveran/spawner).
|
47
48
|
|
48
49
|
It is a common pattern to set expectations for method calls. You can do
|
49
50
|
it with the `expect` function:
|
data/lib/override.rb
CHANGED
@@ -1,19 +1,21 @@
|
|
1
1
|
require "rubygems"
|
2
2
|
require "metaid"
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
module Override
|
5
|
+
def override object, methods
|
6
|
+
methods.each do |method, result|
|
7
|
+
result.respond_to?(:to_proc) ?
|
8
|
+
object.meta_def(method, &result.to_proc) :
|
9
|
+
object.meta_def(method) { |*_| result }
|
10
|
+
end
|
11
|
+
object
|
9
12
|
end
|
10
|
-
object
|
11
|
-
end
|
12
13
|
|
13
|
-
def expect object, method, options
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
def expect object, method, options
|
15
|
+
expectation = lambda do |*params|
|
16
|
+
raise ArgumentError unless params == options[:with]
|
17
|
+
options[:return]
|
18
|
+
end
|
19
|
+
override(object, method => expectation)
|
17
20
|
end
|
18
|
-
override(object, method => expectation)
|
19
21
|
end
|
data/test/all_test.rb
CHANGED