bogus 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. data/.gitignore +7 -0
  2. data/.pelusa.yml +1 -0
  3. data/.travis.yml +6 -0
  4. data/Gemfile +4 -0
  5. data/Gemfile.lock +75 -0
  6. data/Guardfile +15 -0
  7. data/README.md +272 -0
  8. data/Rakefile +1 -0
  9. data/bogus.gemspec +34 -0
  10. data/features/configuration_options.feature +43 -0
  11. data/features/contract_tests_mocks.feature +100 -0
  12. data/features/contract_tests_spies.feature +72 -0
  13. data/features/contract_tests_stubs.feature +101 -0
  14. data/features/fake_objects.feature +94 -0
  15. data/features/return_value_contracts.feature +90 -0
  16. data/features/safe_mocking.feature +59 -0
  17. data/features/safe_stubbing.feature +62 -0
  18. data/features/spies.feature +78 -0
  19. data/features/step_definitions/rspec_steps.rb +68 -0
  20. data/features/support/env.rb +1 -0
  21. data/lib/bogus.rb +35 -0
  22. data/lib/bogus/adds_recording.rb +11 -0
  23. data/lib/bogus/configuration.rb +9 -0
  24. data/lib/bogus/contract_not_fulfilled.rb +24 -0
  25. data/lib/bogus/converts_name_to_class.rb +31 -0
  26. data/lib/bogus/copies_classes.rb +44 -0
  27. data/lib/bogus/creates_fakes.rb +32 -0
  28. data/lib/bogus/double.rb +10 -0
  29. data/lib/bogus/fake.rb +33 -0
  30. data/lib/bogus/fake_registry.rb +13 -0
  31. data/lib/bogus/injector.rb +64 -0
  32. data/lib/bogus/interaction.rb +24 -0
  33. data/lib/bogus/interaction_presenter.rb +29 -0
  34. data/lib/bogus/interactions_repository.rb +19 -0
  35. data/lib/bogus/invocation_matcher.rb +27 -0
  36. data/lib/bogus/method_stringifier.rb +31 -0
  37. data/lib/bogus/overwrites_classes.rb +9 -0
  38. data/lib/bogus/proxy_class.rb +22 -0
  39. data/lib/bogus/public_methods.rb +46 -0
  40. data/lib/bogus/record_interactions.rb +17 -0
  41. data/lib/bogus/recording_proxy.rb +18 -0
  42. data/lib/bogus/records_double_interactions.rb +11 -0
  43. data/lib/bogus/registers_created_fakes.rb +11 -0
  44. data/lib/bogus/rr_proxy.rb +5 -0
  45. data/lib/bogus/rspec.rb +4 -0
  46. data/lib/bogus/rspec_extensions.rb +32 -0
  47. data/lib/bogus/takes.rb +8 -0
  48. data/lib/bogus/verifies_contracts.rb +12 -0
  49. data/lib/bogus/verifies_stub_definition.rb +37 -0
  50. data/lib/bogus/version.rb +3 -0
  51. data/pelusa.sh +3 -0
  52. data/rbs.sh +3 -0
  53. data/spec/bogus/adds_recording_spec.rb +33 -0
  54. data/spec/bogus/configuration_spec.rb +17 -0
  55. data/spec/bogus/converts_name_to_class_spec.rb +40 -0
  56. data/spec/bogus/copies_classes_spec.rb +175 -0
  57. data/spec/bogus/creates_fakes_spec.rb +59 -0
  58. data/spec/bogus/double_spec.rb +31 -0
  59. data/spec/bogus/fake_registry_spec.rb +24 -0
  60. data/spec/bogus/interaction_spec.rb +68 -0
  61. data/spec/bogus/interactions_repository_spec.rb +50 -0
  62. data/spec/bogus/invocation_matcher_spec.rb +26 -0
  63. data/spec/bogus/mocking_dsl_spec.rb +76 -0
  64. data/spec/bogus/overwrites_classes_spec.rb +26 -0
  65. data/spec/bogus/proxy_class_spec.rb +95 -0
  66. data/spec/bogus/record_interactions_spec.rb +27 -0
  67. data/spec/bogus/records_double_interactions_spec.rb +25 -0
  68. data/spec/bogus/registers_created_fakes_spec.rb +25 -0
  69. data/spec/bogus/verifies_contracts_spec.rb +42 -0
  70. data/spec/bogus/verifies_stub_definition_spec.rb +71 -0
  71. data/spec/spec_helper.rb +14 -0
  72. metadata +299 -0
@@ -0,0 +1,7 @@
1
+ *.gem
2
+ .bundle
3
+ pkg/*
4
+ tmp/*
5
+ .rvmrc
6
+ tags
7
+ .rbx
@@ -0,0 +1 @@
1
+ sources: lib/**/*.rb
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - rbx-19mode
6
+ script: bundle exec rspec spec && bundle exec cucumber --tag ~@wip
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in bogus.gemspec
4
+ gemspec
@@ -0,0 +1,75 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bogus (0.0.1)
5
+ dependor (>= 0.0.4)
6
+ rr
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ aruba (0.4.11)
12
+ childprocess (>= 0.2.3)
13
+ cucumber (>= 1.1.1)
14
+ ffi (>= 1.0.11)
15
+ rspec (>= 2.7.0)
16
+ builder (3.0.0)
17
+ childprocess (0.3.2)
18
+ ffi (~> 1.0.6)
19
+ cucumber (1.2.1)
20
+ builder (>= 2.1.2)
21
+ diff-lcs (>= 1.1.3)
22
+ gherkin (~> 2.11.0)
23
+ json (>= 1.4.6)
24
+ dependor (0.0.5)
25
+ diff-lcs (1.1.3)
26
+ ffi (1.0.11)
27
+ gherkin (2.11.0)
28
+ json (>= 1.4.6)
29
+ growl (1.0.3)
30
+ guard (1.1.1)
31
+ listen (>= 0.4.2)
32
+ thor (>= 0.14.6)
33
+ guard-cucumber (1.1.0)
34
+ cucumber (>= 1.2.0)
35
+ guard (>= 1.1.0)
36
+ guard-rspec (1.1.0)
37
+ guard (>= 1.1)
38
+ json (1.7.3)
39
+ libnotify (0.7.4)
40
+ ffi (~> 1.0.11)
41
+ listen (0.4.5)
42
+ rb-fchange (~> 0.0.5)
43
+ rb-fsevent (~> 0.9.1)
44
+ rb-inotify (~> 0.8.8)
45
+ rake (0.9.2.2)
46
+ rb-fchange (0.0.5)
47
+ ffi
48
+ rb-fsevent (0.9.1)
49
+ rb-inotify (0.8.8)
50
+ ffi (>= 0.5.0)
51
+ rr (1.0.4)
52
+ rspec (2.10.0)
53
+ rspec-core (~> 2.10.0)
54
+ rspec-expectations (~> 2.10.0)
55
+ rspec-mocks (~> 2.10.0)
56
+ rspec-core (2.10.1)
57
+ rspec-expectations (2.10.0)
58
+ diff-lcs (~> 1.1.3)
59
+ rspec-mocks (2.10.1)
60
+ thor (0.15.3)
61
+
62
+ PLATFORMS
63
+ ruby
64
+
65
+ DEPENDENCIES
66
+ aruba
67
+ bogus!
68
+ cucumber
69
+ growl
70
+ guard
71
+ guard-cucumber
72
+ guard-rspec
73
+ libnotify
74
+ rake
75
+ rspec
@@ -0,0 +1,15 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'cucumber' do
5
+ watch(%r{^features/.+\.feature$})
6
+ watch(%r{^features/support/.+$}) { 'features' }
7
+ watch(%r{^features/step_definitions/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' }
8
+ #watch(%r{^lib/(.+)\.rb$}) { 'features' }
9
+ end
10
+
11
+ guard 'rspec', :version => 2 do
12
+ watch(%r{^spec/.+_spec\.rb$})
13
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
14
+ watch('spec/spec_helper.rb') { "spec" }
15
+ end
@@ -0,0 +1,272 @@
1
+ # Bogus
2
+
3
+ [![build status](https://secure.travis-ci.org/psyho/bogus.png)](http://travis-ci.org/psyho/bogus)
4
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/psyho/bogus)
5
+
6
+ ## What is Bogus?
7
+
8
+ Bogus aims to make your unit tests more reliable by ensuring that you don't stub or mock methods that don't actually exist in the mocked objects.
9
+
10
+ ## What are fakes?
11
+
12
+ Fakes are alternative implementations of classes for testing purposes.
13
+ In most cases, fakes are just named stubs: they don't implement the logic (otherwise they should be tested too), but they have the same interface as the real object and they make it easier to remove duplication in your stubs.
14
+
15
+ ## Stubs and mocks scatter the interface specification all over your specs
16
+
17
+ Let's assume that you have a couple of classes that share a collaborator. In this case we have a finds_users object that allows finding users by location and suggests_followings and lists_users_near_city that use it in turn to compute a list of users that match some criteria.
18
+
19
+ ```ruby
20
+ class FindsUsers
21
+ takes :users_repository
22
+
23
+ def near_point(location, distance_in_km);
24
+ # do something with users_repository and return a list of users
25
+ end
26
+ end
27
+
28
+ class Geocoder
29
+ takes :http_client
30
+
31
+ def location_for(address)
32
+ # do something to http_client and return location
33
+ end
34
+ end
35
+
36
+ class SuggestsFollowings
37
+ takes :finds_users
38
+
39
+ def suggestions_for(user)
40
+ finds_users.near_point(user.location, 50).select{|user| user.followers_count > 10}
41
+ end
42
+ end
43
+
44
+ class ListsUsersNearCity
45
+ takes :finds_users, :geocoder
46
+
47
+ def users(city)
48
+ city_location = geocoder.location_for(city.name)
49
+ finds_users.near_point(city_location, 10)
50
+ end
51
+ end
52
+ ```
53
+
54
+ Typically, when testing SuggestsFollowings and ListsUsersNearCity you'd have code like this in your specs:
55
+
56
+ ```ruby
57
+ describe SuggestsFollowings
58
+ let(:finds_users) { stub(:finds_users) }
59
+ let(:user) { new_user }
60
+
61
+ subject { SuggestsFollowings.new(finds_users) }
62
+
63
+ it "returns users with follower count of 10 or more" do
64
+ user_with_followers = new_user(followers_count: 10)
65
+ users = [new_user(followers_count: 5), user_with_followers]
66
+
67
+ finds_users.should_receive(:near_point).with(user.location, 50).and_return(users)
68
+
69
+ subject.suggestions_for(user).should == [user_with_followers]
70
+ end
71
+
72
+ # ...
73
+ end
74
+
75
+ describe ListsUsersNearCity
76
+ let(:finds_users) { stub(:finds_users) }
77
+ let(:geocoder) { stub(:geocoder) }
78
+ let(:city) { new_city(name: "New York") }
79
+
80
+ subject { ListsUsersNearCity.new(finds_users, geocoder) }
81
+
82
+ it "returns users near center of the city" do
83
+ location = Location.new(1, 2)
84
+ users = [new_user, new_user]
85
+
86
+ geocoder.should_receive(:location_for).with("New York").and_return(location)
87
+ finds_users.should_receive(:near_point).with(location, 10).and_return(users)
88
+
89
+ subject.users(city).should == users
90
+ end
91
+
92
+ # ...
93
+ end
94
+ ```
95
+
96
+ Notice how the stubbing/mocking code is duplicated between both specs.
97
+ This might not be a huge problem in a small project, but as your codebase grows, there might be more and more objects that interact with finds_users, and the more objects interact with finds_users, the more places it's interface is specified in using stubbing or mock expectations.
98
+
99
+ ## Bogus to the rescue!
100
+
101
+ Bogus is aimed to help you remove duplication in specifying interfaces from your specs.
102
+ It will reduce the need for integration testing, because with Bogus, you make sure that the test doubles you use have the same interface as the real object, while still allowing you to test in isolation and benefit from that.
103
+
104
+ So how would your tests look like if you used Bogus?
105
+
106
+ ```ruby
107
+ require 'bogus/rspec'
108
+
109
+ Bogus.configure do |config|
110
+ config.spy_by_default = true
111
+ config.stub_dsl = :rr # only :rr available for now
112
+ end
113
+
114
+ shared_context 'fakes' do
115
+ fake(:geocoder) # same as let(:geocoder) { Bogus.fake_for(:geocoder) }
116
+ fake(:finds_users)
117
+ end
118
+
119
+ describe SuggestsFollowings
120
+ include_context "fakes"
121
+
122
+ let(:user) { new_user }
123
+
124
+ subject { SuggestsFollowings.new(finds_users) }
125
+
126
+ it "returns users with follower count of 10 or more" do
127
+ user_with_followers = new_user(followers_count: 10)
128
+ users = [new_user(followers_count: 5), user_with_followers]
129
+
130
+ # this will actually fail if FindsUsers does not have a near_point method
131
+ # that takes 2 arguments
132
+ mock(finds_users).near_point(user.location, 50) { users }
133
+
134
+ subject.suggestions_for(user).should == [user_with_followers]
135
+ end
136
+
137
+ # ...
138
+ end
139
+
140
+ describe ListsUsersNearCity
141
+ include_context "fakes"
142
+
143
+ let(:city) { new_city(name: "New York") }
144
+
145
+ subject { ListsUsersNearCity.new(finds_users, geocoder) }
146
+
147
+ it "returns users near center of the city" do
148
+ location = Location.new(1, 2)
149
+ users = [new_user, new_user]
150
+
151
+ mock(geocoder).location_for("New York") { location }
152
+ mock(finds_users).near_point(location, 10) { users }
153
+
154
+ subject.users(city).should == users
155
+ end
156
+
157
+ # ...
158
+ end
159
+ ```
160
+
161
+ As you can see, by using Bogus you can keep writing truly isolated unit tests just like you are used to, but with higher degree of security, because Bogus makes sure, that the methods you are stubbing actually exist and take same arguments.
162
+
163
+ ## Bogus and commands
164
+
165
+ By default, fakes created by Bogus are nil-objects.
166
+ This is particularly useful when dealing with "command" methods or if you follow the "tell, don't ask" principle.
167
+
168
+ ```ruby
169
+ class SendsEmailNotifications
170
+ def notify_password_changed(user)
171
+ # do something
172
+ end
173
+ end
174
+
175
+ class Logger
176
+ def info(*message)
177
+ end
178
+ end
179
+
180
+ class ChangesPassword
181
+ takes :sends_email_notifications, :logger
182
+
183
+ def change_password(user, old_pass, new_pass)
184
+ # change user password
185
+ user.password = new_pass if user.password == old_pass
186
+
187
+ # notify by email
188
+ sends_email_notifications.notify_password_changed(user)
189
+
190
+ # and log
191
+ logger.info("User", user.name, "changed password")
192
+ end
193
+ end
194
+
195
+ describe ChangesPassword do
196
+ fake(:sends_email_notifications)
197
+ fake(:logger)
198
+
199
+ let(:user) { new_user }
200
+
201
+ subject{ ChangesPassword.new(sends_email_notifications, logger) }
202
+
203
+ it "sends the email notification" do
204
+ subject.change_password(user, 'old', 'new')
205
+
206
+ # no need to set up the stub on sends_email_notifications for this to work
207
+ sends_email_notifications.should have_received.notify_password_changed(user)
208
+ end
209
+ end
210
+ ```
211
+
212
+ The beautiful thing is, that even if you don't care about testing something (like logging in this example), you still get the benefit of ensuring that the interface matches, even in a fully isolated test.
213
+
214
+ ## Contract tests
215
+
216
+ Knowing that a method exists and takes the right number of parameters is great, but why stop there?
217
+ Bogus can also make sure that the interface you stub is actually tested somewhere with the correct arguments and return value.
218
+
219
+ ```ruby
220
+ describe ChangesPassword do
221
+ fake(:sends_email_notifications)
222
+ fake(:logger)
223
+
224
+ let(:user) { new_user }
225
+
226
+ subject{ ChangesPassword.new(sends_email_notifications, logger) }
227
+
228
+ it "sends the email notification" do
229
+ subject.change_password(user, 'old', 'new')
230
+
231
+ sends_email_notifications.should have_received.notify_password_changed(user)
232
+ end
233
+ end
234
+ ```
235
+
236
+ In the above spec, SendsEmailNotifications#notify_password_changed is specified as a method that takes a user as an argument.
237
+ Thanks to Bogus, we already know that there is a class named SendsEmailNotifications and it has a #notify_password_changed method that takes one argument.
238
+ But how can we be sure that this argument is a User, and not a String for example?
239
+
240
+ Here's how Bogus can help with this problem:
241
+
242
+ ```ruby
243
+ describe SendsEmailNotifications do
244
+ verify_contract(:sends_email_notifications)
245
+
246
+ let(:sends_email_notifications) { SendsEmailNotifications.new }
247
+
248
+ it "send email notifications" do
249
+ user = new_user
250
+
251
+ sends_email_notifications.notify_password_changed(user)
252
+
253
+ # ...
254
+ end
255
+ end
256
+ ```
257
+
258
+ ## License
259
+
260
+ MIT. See the LICENSE file.
261
+
262
+ ## TODO:
263
+
264
+ This is a README driven project, so don't expect stuff in the README to be implemented ;P
265
+
266
+ See the features directory for the completed functionality.
267
+
268
+ ## Authors
269
+
270
+ * [Adam Pohorecki](http://github.com/psyho)
271
+ * [Paweł Pierzchała](http://github.com/wrozka)
272
+ * [Marek Nowak](https://github.com/yundt)
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "bogus/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "bogus"
7
+ s.version = Bogus::VERSION
8
+ s.authors = ["Adam Pohorecki"]
9
+ s.email = ["adam@pohorecki.pl"]
10
+ s.homepage = "https://github.com/psyho/bogus"
11
+ s.summary = %q{Create fakes to make your isolated unit tests reliable.}
12
+ s.description = %q{Decreases the need to write integration tests by ensuring that the things you stub or mock actually exist.}
13
+
14
+ s.rubyforge_project = "bogus"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency 'dependor', '>= 0.0.4'
22
+ s.add_dependency 'rr'
23
+
24
+ s.add_development_dependency 'rake'
25
+ s.add_development_dependency 'rspec'
26
+ s.add_development_dependency 'cucumber'
27
+ s.add_development_dependency 'aruba'
28
+
29
+ s.add_development_dependency 'guard'
30
+ s.add_development_dependency 'guard-rspec'
31
+ s.add_development_dependency 'guard-cucumber'
32
+ s.add_development_dependency 'growl'
33
+ s.add_development_dependency 'libnotify'
34
+ end
@@ -0,0 +1,43 @@
1
+ Feature: Configuration Options
2
+
3
+ Bogus can be configured, similarly to many other frameworks with a configure block.
4
+ This feature describes the configuration options available.
5
+
6
+ Scenario: search_modules
7
+ Given a file named "foo.rb" with:
8
+ """ruby
9
+ class Foo
10
+ def foo
11
+ end
12
+ end
13
+
14
+ module Bar
15
+ class Baz
16
+ def baz
17
+ end
18
+ end
19
+ end
20
+ """
21
+
22
+ Then spec file with following content should pass:
23
+ """ruby
24
+
25
+ Bogus.configure do |c|
26
+ c.search_modules << Bar
27
+ end
28
+
29
+ describe "logger class fake" do
30
+ fake(:foo)
31
+ fake(:baz)
32
+
33
+ it "finds classes in global namespace" do
34
+ foo.foo
35
+ end
36
+
37
+ it "finds classes in specified modules" do
38
+ baz.baz
39
+ end
40
+ end
41
+ """
42
+
43
+