rr 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,10 @@
1
+ *.iml
2
+ *.ipr
3
+ *.iws
4
+ *.swp
5
+ *.gem
6
+ .idea
7
+ previous_failures.txt
8
+ pkg
9
+ pmip
10
+ scratch.rb
data/.runrc ADDED
@@ -0,0 +1,3 @@
1
+ #!/bin/bash
2
+ . /usr/local/rvm/scripts/rvm
3
+ __rvm_project_rvmrc $(pwd)
data/.rvmrc ADDED
@@ -0,0 +1,2 @@
1
+ rvm 1.9.2-p180@rr
2
+ #rvm ree@rr
@@ -222,7 +222,7 @@ Allows stubs to be added to all instances of a class. It works by binding to met
222
222
  This allows all instances (excluding instances with the method redefined in the eigenclass) to get the change.
223
223
 
224
224
  Due to Ruby runtime limitations, mocks will not work as expected. It's not obviously feasible (without an ObjectSpace lookup) to support all of RR's methods (such as mocking).
225
- ObjectSpace is not readily supported in jRuby, since it causes general slowness in the intreperter.
225
+ ObjectSpace is not readily supported in jRuby, since it causes general slowness in the interpreter.
226
226
  I'm of the opinion that test speed is more important than having mocks on all instances of a class.
227
227
  If there is another solution, I'd be willing to add it.
228
228
 
@@ -0,0 +1,32 @@
1
+ dir = File.dirname(__FILE__)
2
+ require File.expand_path("#{dir}/../lib/rr")
3
+ require "benchmark"
4
+
5
+ o = Object.new
6
+
7
+ Benchmark.bm do |x|
8
+ x.report do
9
+ 1000.times do
10
+ RR.mock(o).foobar.returns("baz")
11
+ o.foobar
12
+ RR.reset
13
+ end
14
+ end
15
+ end
16
+
17
+ #require "ruby-prof"
18
+ #RubyProf.start
19
+ #
20
+ ##RR.mock(o).foobar.returns("baz")
21
+ ##o.foobar
22
+ #10.times do
23
+ # RR.mock(o).foobar.returns("baz")
24
+ # o.foobar
25
+ # RR.reset
26
+ #end
27
+ #
28
+ #result = RubyProf.stop
29
+ #
30
+ ## Print a flat profile to text
31
+ #printer = RubyProf::FlatPrinter.new(result)
32
+ #printer.print(STDOUT, 0)
@@ -0,0 +1,14 @@
1
+ require "rubygems"
2
+ require "spec/mocks"
3
+ require "benchmark"
4
+
5
+ o = Object.new
6
+
7
+ Benchmark.bm do |x|
8
+ x.report do
9
+ 1000.times do
10
+ o.should_receive(:foobar).and_return("baz")
11
+ o.foobar
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,81 @@
1
+ I'm pleased to announce the 0.6.0 version of RR. The [changes](http://github.com/btakita/rr/tree/master%2FCHANGES?raw=true) include:
2
+
3
+ * Declaring Double subject objects without having to pass it in via the mock!, stub!, dont_allow!, instance_of!, and proxy! methods
4
+ * Revised Double chaining API
5
+ * satisfy matcher
6
+ * hash_including matcher
7
+
8
+ # Declaring Double Subjects (The bang methods)
9
+
10
+ In previous versions of RR, you always needed to pass in the subject of the double. For example:
11
+
12
+ subject = Object.new
13
+ mock(subject).does_something {:and_returns_me}
14
+ subject.does_something # :and_returns_me
15
+
16
+ Now you can have RR automatically create the subject object for you by using the ! method:
17
+
18
+ subject = mock!.does_something {:and_returns_me}.subject
19
+ subject.does_something # :and_returns_me
20
+
21
+ Now the bang methods by themselves don't really add a whole lot, but when used in the context of Double chaining, they become a powerful addition.
22
+
23
+ # Double Chaining
24
+
25
+ Nick Kallen presented the use case for Double chaining and contributed a patch for the 0.5.0 release of RR. It has proved useful and is now more fully incorporated into RR. Now you can pass in your subject or use the subject provided by RR by using the ! method. Here are some examples of Double Chaining:
26
+
27
+ mock(subject).first(1) {mock(Object.new).second(2) {mock(Object.new).third(3) {4}}}
28
+ subject.first(1).second(2).third(3) # 4
29
+
30
+ mock(subject).first(1) {mock!.second(2) {mock!.third(3) {4}}}
31
+ subject.first(1).second(2).third(3) # 4
32
+
33
+ mock(subject).first(1).mock!.second(2).mock!.third(3) {4}
34
+ subject.first(1).second(2).third(3) # 4
35
+
36
+ Of course you have access to the proxy facilities:
37
+
38
+ mock.proxy(User).find('1').mock.proxy!.children.mock.proxy!.find_all_by_group_id(10)
39
+ User.find('1').children.find_all_by_group_id(10) # Makes verifications pass and returns the actual children
40
+
41
+ You can also do branched Double chaining:
42
+
43
+ mock(subject).first do
44
+ mock! do |expect|
45
+ expect.branch1.mock!.branch11 {11} # or expect.branch1 {mock!.branch11 {11}}
46
+ expect.branch2.mock!.branch22 {22} # or expect.branch2 {mock!.branch22 {22}}
47
+ end
48
+ end
49
+ o = subject.first
50
+ o.branch1.branch11 # 11
51
+ o.branch2.branch22 # 22
52
+
53
+ # Satisfy Matcher
54
+
55
+ Matthew O'Conner submitted a patch that added the satisfy matcher. This adds the ability to add arbitrary argument expectation matchers.
56
+
57
+ mock(object).foobar(satisfy {|arg| arg.length == 2})
58
+ object.foobar("xy")
59
+
60
+
61
+ # Hash Including Matcher
62
+
63
+ Matthew O'Conner also submitted a patch that added the hash_including matcher. This adds a convenient way to assert that the passed-in hash includes certain key/value pairs.
64
+
65
+ mock(object).foobar(hash_including(:red => "#FF0000", :blue => "#0000FF"))
66
+ object.foobar({:red => "#FF0000", :blue => "#0000FF", :green => "#00FF00"})
67
+
68
+ # Mailing list
69
+
70
+ RR has a mailing lists at:
71
+
72
+ * [double-ruby-users@rubyforge.org](mailto:double-ruby-users@rubyforge.org)
73
+ * [double-ruby-devel@rubyforge.org](mailto:double-ruby-devel@rubyforge.org)
74
+
75
+ Also, RR's rubyforge page is at [http://rubyforge.org/projects/double-ruby](http://rubyforge.org/projects/double-ruby) and of course the github page is at [http://github.com/btakita/rr](http://github.com/btakita/rr).
76
+
77
+ # Yes, and there is more to come
78
+
79
+ There are many interesting ideas floating around. Joseph Wilk has been playing around with [adding Spies](http://github.com/JoeSniff/rr) into RR. I'm also thinking about adding Double validation scoping into RR. Also, I'm impressed by Mocha's warning of unused stubs. Josh Susser also proposed having a mode where a warning would occur if a mocked method is not implemented on the subject being mocked.
80
+
81
+ If you have any feature requests, please send an email to the mailing list or add it to the rubyforge tracker.
File without changes
@@ -0,0 +1,206 @@
1
+ **Introducting RR**
2
+
3
+ I'm pleased to introduce a new Test Double framework names RR, which is short for Double Ruby.
4
+ A Test Double is [double description]. You can read more about test doubles at http://xunitpatterns.com/Test%20Double.html.
5
+
6
+ RR supports the following constructs:
7
+ * Mock
8
+ * Stub
9
+ * instance_of
10
+ * Probe
11
+
12
+ **Mock**
13
+ <pre>
14
+ real_user = User.new
15
+ mock(User).find('2') {real_user}
16
+ </pre>
17
+
18
+ The previous example overrides the User.find method and returns real_user. It also sets an expectation
19
+ that the find method will receive the argument '2' once.
20
+
21
+ **Stub**
22
+ <pre>
23
+ user = User.new
24
+ my_article = articles(:my_article)
25
+ stub(user).can_edit?(my_article) {true}
26
+ </pre>
27
+
28
+ The previous example overrides can_edit?. When the method receives the article, it returns true.
29
+
30
+ **instance_of**
31
+ You can mock or stub instances of a class.
32
+ <pre>
33
+ stub.instance_of(User).can_edit?(my_article) {true}
34
+ </pre>
35
+
36
+ The previous example stubs the can_edit? method of any intstance of User.
37
+
38
+ **Probe**
39
+
40
+ A probe is a test double strategy that lets the real method implementation be called, and allows you
41
+ to intercept the return value, and possibly inject you own replacement return value.
42
+ <pre>
43
+ my_article = articles(:my_article)
44
+ mock.probe(User).find('2') do |real_user|
45
+ stub.probe(real_user).can_edit?(my_article) {true}
46
+ real_user
47
+ end
48
+ </pre>
49
+
50
+ The previous example, lets the real User.find method call happen, and intercepts its return value inside
51
+ of the block.
52
+
53
+ The real_user's can_edit? method is then stubbed and probed to return true.
54
+
55
+ **Thats nice, how is it useful?**
56
+
57
+ As with any tool, Mocks and Stubs have limitations.
58
+ For example, mocks alone do not verify that the mocked out method conforms to the real object's interface.
59
+ Probes solve this issue.
60
+
61
+ Adding a probe ensures that:
62
+ * The method call to User.find is valid
63
+ * The return value of User.find is available to validate and/or add test doubles to
64
+ * The method call to real_user.can_edit? is valid
65
+
66
+ **I don't use Mocks. Why should I care?**
67
+
68
+ State based testing is often the simplest and most straightforward way to make assertions.
69
+ However Interaction assertions can serve as good documentation of how your system fits together.
70
+ Interaction tests can also aid you in making your tests easier to set up and removing coupling between tests.
71
+
72
+ Lets compare the state and interaction testing approaches in the can edit article example for the
73
+ ArticlesController#edit action:
74
+
75
+ **State Based Example**
76
+ <pre>
77
+ user = users(:bob)
78
+ login(user)
79
+ my_article = articles(:my_article)
80
+ user.can_edit?(my_article).should == false
81
+
82
+ lambda do
83
+ post :edit, :id => my_article.id, :body => "Hello everybody"
84
+ end.should raise_error(SecurityTrangressionError)
85
+ </pre>
86
+
87
+ **Interaction Based Example**
88
+ <pre>
89
+ user = users(:bob)
90
+ login(user)
91
+ my_article = articles(:my_article)
92
+ mock.probe(user).can_edit? {false}
93
+
94
+ lambda do
95
+ post :edit, :id => my_article.id, :body => "Hello everybody"
96
+ end.should raise_error(SecurityTrangressionError)
97
+ </pre>
98
+
99
+ These two examples are interesting because they verify slight different things.
100
+ The interaction example states that when can_edit? with @article is called and returns false, a SecurityTrangressionError
101
+ is raised.
102
+ The state example gives information that bob cannot edit the article, and from that one can infer that
103
+ bob trying to edit the article will raise a SecurityTrangressionError.
104
+
105
+ State based testing tends to be more coupling than interaction based testing.
106
+ Note that coupling is not necessarily bad.
107
+ The state based example has both interface, data, and knowledge coupling compared to the interaction based test:
108
+ * Interface coupling - If can_edit? does not return false, there is an error.
109
+ * Fixture Data coupling - If the fixture data changes, there is an error.
110
+ * Knowledge coupling - The ArticleController test needs to know how can_edit? returns false
111
+
112
+ Interface coupling is actually a good thing, because it verifies the User and ArticleController work
113
+ together proberly. This sort of testing is functional or integration testing.
114
+
115
+ The Data and Knowledge coupling are not desirable characteristics because they cause the
116
+ developer to be concerned about another part of the system, which takes development time.
117
+ It can also cause unwanted test failures when the global fixture data is changed or when a change
118
+ occurs that makes the ArticleController's test setup logic incorrect.
119
+
120
+ Martin Fowler also notes that interaction testing has coupling to the edit action's implementation,
121
+ in that the interaction example states that User#can_edit? must be called for the test to pass.
122
+ I've found that sometimes this is desirable and sometimes it is not desirable.
123
+
124
+ The coupling to the implementation encourages more decomposition but it also makes
125
+ causes brittleness because changing the edit action to call another method will cause
126
+ the test to fail.
127
+
128
+ I'm not proposing the right solution in this case, because it is dependent on
129
+ your situation. One thing to consider is:
130
+ * Do you have functional or integration tests for the failure case?
131
+
132
+ Taking the desirable and undesirable coupling into account
133
+
134
+ Here is a view example that renders counts:
135
+
136
+ **State Based Example**
137
+ <pre>
138
+ user = users(:bob)
139
+ user.articles.count.should == 5
140
+ user.articles.comments.count.should == 15
141
+ user.gold_stars.count.should == 0
142
+
143
+ render :template => "users/show"
144
+ response.body.should include("Articles Posted: 5")
145
+ response.body.should include("Comments Received: 15")
146
+ response.body.should include("Gold Stars Received: 0")
147
+ </pre>
148
+
149
+ **Interaction Based Example**
150
+ <pre>
151
+ user = User.new
152
+ mock.probe(user.articles).count {5}
153
+ mock.probe(user.articles.comments).count {15}
154
+ mock.probe(user.gold_stars).count {80}
155
+
156
+ render :template => "users/show"
157
+ response.body.should include("Articles Posted: 5")
158
+ response.body.should include("Comments Received: 15")
159
+ response.body.should include("Gold Stars Received: 80")
160
+ </pre>
161
+
162
+ The same tradeoffs are present in this view example as in the ActiclesController example,
163
+ but the values of each of the tradeoffs are different.
164
+
165
+ State testing couplings:
166
+ * Interface coupling - If count returns a non-number, there is an error.
167
+ * Fixture Data coupling - If the fixture data changes, there is an error.
168
+ * Knowledge coupling - There is no noticeable knowledge coupling.
169
+
170
+ Interaction testing couplings:
171
+ * Implementation coupling - If the way the count is determined changes, there is an error.
172
+
173
+ In these examples, it is fair to expect the counts derived from the fixtures to change quite often.
174
+ Decoupling the counts from your fixtures yields more of a benefit because the interaction based example
175
+ will probably not need to be changed as often as the state based example.
176
+
177
+ The interaction based example also provides the benefits of:
178
+ * being faster because there is no database access
179
+ * providing more focus because non-count user data is not important to this example (the interaction example
180
+ ignores the user data while the state based approach includes the user data)
181
+ * not requiring you to change the fixture data to provide add "Gold Stars Received" because having
182
+ "Gold Stars Received: 0" is almost meaningless (It could easily be calling count on something else
183
+ that returns 0)
184
+
185
+ **State vs. Interaction Based testing?**
186
+
187
+ The examples I provided favor or are neutral to interaction based testing.
188
+ This does not mean all testing should be done with interaction testing.
189
+ There are many situations where state based testing is more
190
+ straightforward and no more coupled than an interaction based test.
191
+
192
+ Please pick the right toolset for your situation.
193
+ In the future, I will blog about different situations and the trade-offs of
194
+ using a state based approach, and/or an interaction based approach.
195
+
196
+ **Extremely Bad Examples**
197
+
198
+ Since this is a blog post, the examples are short and relatively benign.
199
+ However, There are many examples where state and/or interaction
200
+ based testing is overused and abused.
201
+ Expanding your toolset can help you and your coworkers fix these issues.
202
+
203
+ There are already several nice Mock/Stub frameworks in the ruby world. These libraries include:
204
+ * Mocha
205
+ * Flexmock
206
+ * Rspec's Mock Framework
@@ -111,7 +111,9 @@ module RR
111
111
  end
112
112
 
113
113
  def reset_bound_objects
114
- RR::Injections::DoubleInjection::BoundObjects.clear
114
+ # TODO: Figure out how to clear and reset these bindings
115
+ #RR::Injections::DoubleInjection::BoundObjects.clear
116
+ #RR::Injections::DoubleInjection::MethodMissingInjection.clear
115
117
  end
116
118
  end
117
119
  end
@@ -0,0 +1,37 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rr}
8
+ s.version = "1.0.4"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Brian Takita"]
12
+ s.date = %q{2011-06-16}
13
+ s.description = %q{RR (Double Ruby) is a double framework that features a rich selection of double techniques and a terse syntax. http://xunitpatterns.com/Test%20Double.html}
14
+ s.email = %q{brian@pivotallabs.com}
15
+ s.extra_rdoc_files = [
16
+ "CHANGES",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.homepage = %q{http://pivotallabs.com}
22
+ s.rdoc_options = ["--main", "README.rdoc", "--inline-source", "--line-numbers"]
23
+ s.require_paths = ["lib"]
24
+ s.rubyforge_project = %q{pivotalrb}
25
+ s.rubygems_version = %q{1.6.2}
26
+ s.summary = %q{RR (Double Ruby) is a double framework that features a rich selection of double techniques and a terse syntax. http://xunitpatterns.com/Test%20Double.html}
27
+
28
+ if s.respond_to? :specification_version then
29
+ s.specification_version = 3
30
+
31
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
32
+ else
33
+ end
34
+ else
35
+ end
36
+ end
37
+
@@ -284,7 +284,9 @@ module RR
284
284
  stub(subject).foobar
285
285
  RR::Injections::DoubleInjection::BoundObjects.should_not be_empty
286
286
  space.reset
287
- RR::Injections::DoubleInjection::BoundObjects.should be_empty
287
+ pending "Clearing BoundObjects" do
288
+ RR::Injections::DoubleInjection::BoundObjects.should be_empty
289
+ end
288
290
  end
289
291
  end
290
292
 
@@ -0,0 +1,10 @@
1
+ --colour
2
+ --format
3
+ progress
4
+ --loadby
5
+ mtime
6
+ --reverse
7
+ --timeout
8
+ 20
9
+ --diff
10
+ --backtrace
@@ -10,7 +10,11 @@ Spec::Runner.configure do |config|
10
10
  end
11
11
 
12
12
  describe "Swapped Space", :shared => true do
13
- attr_reader :space, :original_space
13
+ attr_reader :original_space
14
+ unless instance_methods.include?(:space)
15
+ attr_reader :space
16
+ end
17
+
14
18
  before do
15
19
  @original_space = RR::Space.instance
16
20
  RR::Space.instance = RR::Space.new
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rr
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.3
5
+ version: 1.0.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Brian Takita
@@ -24,13 +24,20 @@ extra_rdoc_files:
24
24
  - CHANGES
25
25
  - README.rdoc
26
26
  files:
27
+ - .gitignore
28
+ - .runrc
29
+ - .rvmrc
27
30
  - CHANGES
28
31
  - Gemfile
29
32
  - Gemfile.lock
30
33
  - LICENSE
31
34
  - README.rdoc
32
35
  - Rakefile
33
- - VERSION.yml
36
+ - benchmarks/rr_benchmark.rb
37
+ - benchmarks/rspec_benchmark.rb
38
+ - doc/0.6.0.release.markdown
39
+ - doc/todo.txt
40
+ - introducting_rr.txt
34
41
  - lib/rr.rb
35
42
  - lib/rr/adapters/minitest.rb
36
43
  - lib/rr/adapters/rr_methods.rb
@@ -106,6 +113,7 @@ files:
106
113
  - lib/rr/wildcard_matchers/range.rb
107
114
  - lib/rr/wildcard_matchers/regexp.rb
108
115
  - lib/rr/wildcard_matchers/satisfy.rb
116
+ - rr.gemspec
109
117
  - spec/api/any_instance_of/all_instances_of_spec.rb
110
118
  - spec/api/any_instance_of/any_instance_of_spec.rb
111
119
  - spec/api/any_instance_of/instance_of_spec.rb
@@ -173,6 +181,7 @@ files:
173
181
  - spec/rr/wildcard_matchers/regexp_spec.rb
174
182
  - spec/rr_spec.rb
175
183
  - spec/rspec_spec_suite.rb
184
+ - spec/spec.opts
176
185
  - spec/spec_helper.rb
177
186
  - spec/spec_suite.rb
178
187
  - spec/spy_verification_spec.rb
@@ -208,7 +217,5 @@ rubygems_version: 1.3.9.2
208
217
  signing_key:
209
218
  specification_version: 3
210
219
  summary: RR (Double Ruby) is a double framework that features a rich selection of double techniques and a terse syntax. http://xunitpatterns.com/Test%20Double.html
211
- test_files:
212
- - spec/proc_from_block_spec.rb
213
- - spec/rr_spec.rb
214
- - spec/spy_verification_spec.rb
220
+ test_files: []
221
+
@@ -1,5 +0,0 @@
1
- ---
2
- :minor: 0
3
- :patch: 2
4
- :major: 1
5
- :build: