grasshopper 0.2.0 → 0.3.0

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 +31 -2
  2. data/lib/grasshopper.rb +23 -7
  3. metadata +3 -3
data/README.md CHANGED
@@ -1,8 +1,37 @@
1
1
  # Grasshopper
2
2
 
3
- A tiny mocking framework based on Mockito
3
+ A tiny mocking framework based on [Mockito](http://code.google.com/p/mockito/). I built it so that I could write unit tests in the [Arrage-Act-Assert](http://c2.com/cgi/wiki?ArrangeActAssert) style.
4
4
 
5
- # Naming
5
+ ## Examples
6
+
7
+ For more examples, see the [unit tests](https://github.com/mattraibert/grasshopper/blob/master/test/grasshopper_test.rb)
8
+
9
+ ### Mock
10
+
11
+ Mock is a person who listens carefully but never says anything. You say anything to Mock, but you'll only get crickets back.
12
+
13
+ ```ruby
14
+ mock = Mock.new
15
+ mock.hello #=> nil
16
+ mock.how_are_you? #=> nil
17
+ Mock.verify(mock).hello # yes, someone did tell me hello.
18
+ Mock.verify(mock).goodbye # wait(AssertionFailed)! they left without saying goodbye!
19
+ ```
20
+
21
+ ### Stub
22
+
23
+ Stub is a person who holds something for you and gives it to anyone who knows how to ask. You can ask Stub for anything, but you'll only get what you put into it.
24
+
25
+ ```ruby
26
+ stub = Stub.new
27
+ Stub.when(stub.keys).then_return([:door_key, :mail_key])
28
+ Stub.when(stub.money("gimmie")).then_return(5)
29
+ stub.keys #=> [:door_key, :mail_key]
30
+ stub.money("gimmie") #=> 5
31
+ stub.monkey #=> nil
32
+ ```
33
+
34
+ ## Naming
6
35
 
7
36
  Grasshopper is named for a [restaurant](http://grasshoppervegan.com/) in Allston, MA that serves up all sorts of great mocks.
8
37
 
@@ -24,17 +24,33 @@ class Mock
24
24
  end
25
25
  end
26
26
 
27
+ class StubHelper
28
+ def initialize(stub, message)
29
+ @stub = stub
30
+ @message = message
31
+ end
32
+
33
+ def then_return(retval)
34
+ @stub.add_a_stub(@message, retval)
35
+ end
36
+ end
37
+
27
38
  class Stub
28
- def initialize(stubs)
29
- @stubs = stubs
39
+ def initialize
40
+ @stubs = {}
30
41
  end
31
42
 
32
- def method_missing(sym, *args)
33
- return nil unless @stubs.has_key?(sym)
34
- @stubs[sym]
43
+ def add_a_stub(message, retval)
44
+ @stubs[message] = retval
45
+ end
46
+
47
+ def self.when(whatever)
48
+ StubHelper.new(@@stub, @@message)
35
49
  end
36
50
 
37
- def self.like(name = "a stub", stubs)
38
- Stub.new(stubs)
51
+ def method_missing(sym, *args)
52
+ @@stub = self
53
+ @@message = [sym, args]
54
+ @stubs[[sym, args]]
39
55
  end
40
56
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grasshopper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-01 00:00:00.000000000 Z
12
+ date: 2012-09-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
@@ -101,7 +101,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
101
101
  version: '0'
102
102
  segments:
103
103
  - 0
104
- hash: -2553078079800841230
104
+ hash: 504949561135609211
105
105
  required_rubygems_version: !ruby/object:Gem::Requirement
106
106
  none: false
107
107
  requirements: