rspec-coldep 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 69c210795fa47ca6ed4d38abfd61aa5b7e96dac6
4
+ data.tar.gz: 92d099583fca68f93374903f58f151d0145e6086
5
+ SHA512:
6
+ metadata.gz: f55c2593719069b03299275813c6ed02f356ae78fb1b541219cdde5e32a71900cc08dae5e717bb13742a48380fe65cdfbbfd6b91ff9aa25983ed7d66a061fd62
7
+ data.tar.gz: 87627d9119fc465b076a337680444686b71f891f3c166b090b0b81fb7da56d706aee501d783d3946422742e192c56f6e0b14a67cd881632665ee8c17fe76ea1d
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ *.swp
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rspec-coldep.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Matt Daubert
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # RSpec-ColDep
2
+
3
+ Col(laborators) & Dep(endencies)
4
+
5
+ Syntax for specifing RSpec stubs and test doubles that mimicks how they are used.
6
+
7
+ This is an experiment to see if test setup code can more explicitly document the dependencies and collaborators of objects under test.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'rspec-coldep'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rspec-coldep
22
+
23
+ ## Documenting dependencies stubs constants
24
+
25
+ RSpec-ColDep syntax documents dependencies of the object under test and creates stubs based on the example usage provided via block:
26
+
27
+ dependency 'User' do
28
+ User.find('1234') == 'a single fake user'
29
+ User.all == 'a couple fake users'
30
+ end
31
+
32
+ is effectively the same as:
33
+
34
+ User.stub(:find).with('1234').and_return('a single fake user')
35
+ User.stub(:all).and_return('a couple fake users')
36
+
37
+ ## Documenting collaborators builds test doubles
38
+
39
+ RSpec-ColDep syntax documents a collaborator's interface with the object under test and builds a test double based on the example usage provided via block:
40
+
41
+ fake_user = collaborator do |user|
42
+ user.first_name == 'Billy'
43
+ user.last_name == 'Jean'
44
+ end
45
+
46
+ is effectively the same as:
47
+
48
+ fake_user = double(first_name: 'Billy', last_name: 'Jean')
49
+
50
+ ## How is this beneficial?
51
+
52
+ This is just an experiment and may very well be a terrible idea. The thought that this gem explores is the ability to define dependencies and collaborating objects by writing code that documents how they are used without being litered by mocking framework method names like `stub`, `with`, `and_returns`, etc.
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,37 @@
1
+ module RSpec
2
+ module Coldep
3
+ class StubBuilder
4
+ def initialize(target)
5
+ @target = target
6
+ end
7
+
8
+ def method_missing(method, *args, &block)
9
+ @stub = [method, args]
10
+ self
11
+ end
12
+
13
+ def ==(return_value)
14
+ @target.stub(@stub[0]).with(*@stub[1]).and_return(return_value)
15
+ end
16
+ end
17
+
18
+ module Helpers
19
+ def collaborator(name = nil)
20
+ double(name).tap do |fake|
21
+ yield StubBuilder.new(fake)
22
+ end
23
+ end
24
+
25
+ def dependency(stub_class_name)
26
+ fake = double
27
+ stub_const(stub_class_name, StubBuilder.new(fake))
28
+ yield
29
+ stub_const(stub_class_name, fake)
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ RSpec.configure do |config|
36
+ config.include RSpec::Coldep::Helpers
37
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rspec-coldep"
7
+ spec.version = "0.1.0"
8
+ spec.authors = ["Matt Daubert"]
9
+ spec.email = ["mdaubert@gmail.com"]
10
+ spec.description = %q{RSpec mocking syntax using collaborators and dependencies}
11
+ spec.summary = %q{Mock how you code, code how you mock.}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec"
23
+ end
@@ -0,0 +1 @@
1
+ require 'rspec/coldep'
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'usage' do
4
+ it 'mocks an undefined constant' do
5
+ dependency 'User' do
6
+ User.find('1234') == 'a single fake user'
7
+ User.all == 'a couple fake users'
8
+ end
9
+
10
+ expect(User.find('1234')).to eq('a single fake user')
11
+ expect(User.all).to eq('a couple fake users')
12
+ end
13
+
14
+ it 'creates a test double' do
15
+ user = collaborator do |user|
16
+ user.first_name == 'Billy'
17
+ user.last_name == 'Jean'
18
+ end
19
+
20
+ expect(user.first_name).to eq('Billy')
21
+ expect(user.last_name).to eq('Jean')
22
+ end
23
+
24
+ it 'allows defining collaborators inside of dependencies' do
25
+ dependency 'User' do
26
+ User.find('1234') == collaborator do |user|
27
+ user.permalink == '1234-billy-jean'
28
+ end
29
+ end
30
+
31
+ expect(User.find('1234').permalink).to eq('1234-billy-jean')
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-coldep
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matt Daubert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: RSpec mocking syntax using collaborators and dependencies
56
+ email:
57
+ - mdaubert@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/rspec/coldep.rb
68
+ - rspec-coldep.gemspec
69
+ - spec/spec_helper.rb
70
+ - spec/usage_spec.rb
71
+ homepage: ''
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.0.2
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Mock how you code, code how you mock.
95
+ test_files:
96
+ - spec/spec_helper.rb
97
+ - spec/usage_spec.rb