You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+73-47Lines changed: 73 additions & 47 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
1
# Spy
2
2
3
-
Spy is a lightweight stubbing framework that won't let your code mock your intelligence.
3
+
Spy is a lightweight stubbing framework with support for method spies, constant stubs, and object doubles.
4
4
5
-
Spy was designed for 1.9.3+ so there is no legacy tech debt.
5
+
Spy was designed for 1.9.3+.
6
6
7
7
Spy features that were completed were tested against the rspec-mocks tests so it covers all cases that rspec-mocks does.
8
8
@@ -33,10 +33,7 @@ Fail faster, code faster.
33
33
* missing these features
34
34
* Mocking null objects
35
35
* argument matchers for Spy::Method#has\_been\_called\_with
36
-
* watch all calls to an object to check order in which they are called?
37
-
* is this useful?
38
-
* fail lazily on method call not on hook to allow for dynamic method creation?
39
-
* do more than 0.5% of develoeprs use this?
36
+
* watch all calls to an object to check order in which they are called
40
37
41
38
## Installation
42
39
@@ -54,64 +51,93 @@ Or install it yourself as:
54
51
55
52
## Usage
56
53
54
+
### Method Stubs
55
+
56
+
A method stub overrides a pre-existing method and records all calls to specified method. You can set the spy to return either the original method or your own custom implementation.
57
+
58
+
Spy support 2 different ways of spying an existing method on an object.
57
59
```ruby
58
-
classPerson
59
-
deffirst_name
60
-
"John"
61
-
end
60
+
Spy.on(book, title:"East of Eden")
61
+
Spy.on(book, :title).and_return("East of Eden")
62
+
Spy.on(book, :title).and_return { "East of Eden" }
63
+
```
62
64
63
-
deflast_name
64
-
"Smith"
65
-
end
65
+
Spy will raise an error if you try to stub on a method that doesn't exist.
66
+
You can force the creation of a sstub on method that didn't exist but it really isn't suggested.
If you just want to make sure if a method is called and not override the output you can just use the and\_call\_through method
94
121
95
-
Spy.teardown
96
-
person.first_name #=> "John"
122
+
```ruby
123
+
Spy.on(book, :read_page).and_call_through
124
+
```
97
125
98
-
say_spy =Spy.on(person, :say)
99
-
person.say("hello") {
100
-
"everything accepts a block in ruby"
101
-
}
102
-
say_spy.say("world")
126
+
### Call Logs
103
127
104
-
say_spy.has_been_called? #=> true
105
-
say_spy.has_been_called_with?("hello") #=> true
106
-
say_spy.calls.count #=> 1
107
-
say_spy.calls.first.args #=> ["hello"]
108
-
say_spy.calls.last.args #=> ["world"]
128
+
When a spy is called on it records a call log. A call log contains the object it was called on, the arguments and block that were sent to method and what it returned.
109
129
110
-
call_log = say_spy.calls.first
111
-
call_log.object #=> #<Person:0x00000000b2b858>
112
-
call_log.args #=> ["hello"]
113
-
call_log.block #=> #<Proc:0x00000000b1a9e0>
114
-
call_log.block.call #=> "everything accepts a block in ruby"
0 commit comments