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.
Files changed (3) hide show
  1. data/README.md +58 -42
  2. data/lib/micro_mock/version.rb +1 -1
  3. metadata +3 -3
data/README.md CHANGED
@@ -1,59 +1,75 @@
1
1
  # MicroMock
2
2
 
3
- ### A tiny mocking script.
3
+ ### Perhaps the lightest mocking strategy available
4
4
 
5
- MicroMock doesn't make any assumptions about the testing framework.
6
- It leaves assertions/expectations up to you.
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
- ## Usage
14
+ ## Quick Start
15
+
19
16
  ```ruby
20
- # mock a class method
21
- Mock = MicroMock.make
22
- Mock.stub(:foo) { true }
23
-
24
- # make assertions
25
- assert Mock.foo # Test::Unit
26
- Mock.foo.should be_true # RSpec
27
-
28
- # mock an instance method
29
- m = Mock.new
30
- m.stub(:bar) { false }
31
-
32
- # make assertions
33
- assert_equal false, m.bar # Test::Unit
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
- Of course you wouldn't normally test the mock itself... rather the code that uses the mock.
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
- Mock = MicroMock.make(Array)
53
- list = Mock.new
54
- list << 1
55
- list.stub :say_hi do |name|
56
- "Hi #{name}!"
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
- list.say_hi "Nate" # => "Hi Nate!"
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!
@@ -1,3 +1,3 @@
1
1
  module MicroMock
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
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.6
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-17 00:00:00.000000000 Z
12
+ date: 2012-11-19 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: ! ' MicroMock might just be the lightest mocking strategy available.
14
+ description: ! ' Perhaps the lightest mocking strategy available.
15
15
 
16
16
  '
17
17
  email: