micro_mock 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +58 -42
- data/lib/micro_mock/version.rb +1 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -1,59 +1,75 @@
|
|
1
1
|
# MicroMock
|
2
2
|
|
3
|
-
###
|
3
|
+
### Perhaps the lightest mocking strategy available
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
Calling it a mocking script is a bit of a misnomer
|
9
|
-
since its really a dynamic class generator.
|
10
|
-
The term "stub" is used loosely since it adds real behavior...
|
11
|
-
and "mocking" a class with real behavior proves to be quite useful.
|
5
|
+
Calling it a mocking script is a bit of a misnomer since its really a dynamic class generator. The term "stub" is used loosely since it adds real behavior...
|
6
|
+
and "mocking" with real behavior proves to be quite useful.
|
12
7
|
|
13
8
|
## Intall
|
9
|
+
|
14
10
|
```bash
|
15
11
|
gem install micro_mock
|
16
12
|
```
|
17
13
|
|
18
|
-
##
|
14
|
+
## Quick Start
|
15
|
+
|
19
16
|
```ruby
|
20
|
-
#
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
#
|
33
|
-
|
34
|
-
m.bar.should eq false # RSpec
|
35
|
-
|
36
|
-
# setup mock internal behavior
|
37
|
-
count = 1
|
38
|
-
m.stub(:a) { count += 1 }
|
39
|
-
m.stub(:b) { |i| self.a if i > 5 }
|
40
|
-
|
41
|
-
# make assertions
|
42
|
-
10.times { |i| m.b(i) }
|
43
|
-
assert_equal 5, count # Test::Unit
|
44
|
-
count.should eq 5 # RSpec
|
17
|
+
# create a mock class
|
18
|
+
MyMock = MicroMock.make
|
19
|
+
|
20
|
+
# stub a class method
|
21
|
+
MyMock.stub(:foo) { "foo" }
|
22
|
+
|
23
|
+
# create a mock instance
|
24
|
+
mock = MyMock.new
|
25
|
+
|
26
|
+
# stub an instance method
|
27
|
+
mock.stub(:bar) { "#{self.class.foo}bar" }
|
28
|
+
|
29
|
+
MyMock.foo # => "foo"
|
30
|
+
mock.bar # => "foobar"
|
45
31
|
```
|
46
32
|
|
47
|
-
|
48
|
-
I'll work on adding some real world examples.
|
33
|
+
## Next Steps
|
49
34
|
|
50
35
|
```ruby
|
51
36
|
# create a mock that subclasses Array
|
52
|
-
|
53
|
-
|
54
|
-
list
|
55
|
-
|
56
|
-
|
37
|
+
MockList = MicroMock.make(Array)
|
38
|
+
|
39
|
+
list = MockList.new
|
40
|
+
|
41
|
+
# stub an instance method that does something interesting
|
42
|
+
list.stub :prefixed do |prefix|
|
43
|
+
map { |value| "#{prefix}:#{value}"}
|
57
44
|
end
|
58
|
-
|
45
|
+
|
46
|
+
list.concat [1, 2, 3]
|
47
|
+
list.prefixed(:num) # => ["num:1", "num:2", "num:3"]
|
59
48
|
```
|
49
|
+
|
50
|
+
## Deep Cuts
|
51
|
+
|
52
|
+
Here is an example that mocks part of ActiveRecord.
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
Model = MicroMock.make
|
56
|
+
model = Model.new
|
57
|
+
model.stub(:destroy) { @destroyed = true }
|
58
|
+
model.stub(:destroyed?) { @destroyed }
|
59
|
+
model.stub(:update_attributes) { |*args| @attributes_updated = true }
|
60
|
+
model.stub(:save) { |*args| @saved = true }
|
61
|
+
Model.stub(:find) { |*args| model.clone }
|
62
|
+
Model.stub(:all) { (1..5).map { model.clone } }
|
63
|
+
|
64
|
+
# try it out
|
65
|
+
list = Model.all # => [#<MicroMock70331390241500:0x007fee9b1b1bb0 @args=[]>, #<MicroMock...]
|
66
|
+
m = Model.find(1) # => #<MicroMock70331390241500:0x007fee9b17b6a0 @args=[]>
|
67
|
+
m.update_attributes(:foo, "bar") # => true
|
68
|
+
m.save # => true
|
69
|
+
m.destroy # => true
|
70
|
+
m.destroyed? # => true
|
71
|
+
```
|
72
|
+
|
73
|
+
For a more complete example, check out [Coast's test suite](https://github.com/hopsoft/coast/blob/master/test/test_coast.rb) which mocks a significant portion of Rails.
|
74
|
+
|
75
|
+
Enjoy!
|
data/lib/micro_mock/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: micro_mock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,9 +9,9 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-19 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
|
-
description: ! '
|
14
|
+
description: ! ' Perhaps the lightest mocking strategy available.
|
15
15
|
|
16
16
|
'
|
17
17
|
email:
|