mimic-rb 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format doc
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ langauge: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ ## v0.0.1
2
+
3
+ * initial release
4
+
5
+ ## v0.1.0
6
+ * adding arguement validation
7
+ * initial Gem Release
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source :rubygems
2
+
3
+ gem 'rake'
4
+ gem 'rspec'
5
+ gem 'guard-rspec'
6
+ gem 'rb-fsevent', '~> 0.9.1'
7
+ gem 'pry'
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard 'rspec' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Justin Herrick
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,109 @@
1
+ #Mimic [![Build Status](https://secure.travis-ci.org/jah2488/mimic.png?branch=master)](https://travis-ci.org/jah2488/mimic)
2
+
3
+ ##Rspec style mocking and stubbing for NullObjects, Fakes, and more!##
4
+
5
+ Inspired by [r00k's](http://www.twitter.com/r00k) talk at [magicruby](http://magic-ruby.com/), I decided to work on a minimalistic library for mocking and stubbing full classes. The original purpose would be to have a NullObject consistently stay in sync with its real object equivelant with minimal effort.
6
+
7
+ ## Installing ##
8
+
9
+ ````sh
10
+ gem install mimic
11
+ ````
12
+ or
13
+
14
+ ````ruby
15
+ #Gemfile
16
+ gem 'mimic'
17
+ ````
18
+
19
+ ````sh
20
+ bundle install
21
+ ````
22
+
23
+ ## Usages ##
24
+
25
+ Given we have an awesome class
26
+ ````ruby
27
+ class NameResponder
28
+
29
+ def hello
30
+ "hi"
31
+ end
32
+
33
+ def name
34
+ "John"
35
+ end
36
+
37
+ def full_name(first, last)
38
+ "#{first} #{last}"
39
+ end
40
+
41
+ def self.reverse(name)
42
+ name.reverse
43
+ end
44
+
45
+ end
46
+ ````
47
+
48
+ Then in our NullObject / Fake / Mimic we simply need to include it and use it.
49
+ ````ruby
50
+ class FakeResponder
51
+ include Mimic
52
+ mocks NameResponder
53
+
54
+ stubs(:name) { "None" }
55
+
56
+ class_stubs(:reverse) { "no name" }
57
+ end
58
+ ````
59
+
60
+ Then anywhere we would use `NameResponder` we can use `FakeResponder` and it will respond to all calls. The default return value for all methods is simply `nil`, but with the `stubs(:method) { result }` method you can specify what you want to be returned.
61
+
62
+ ````ruby
63
+
64
+ responder = NameResponder.new
65
+ fake = FakeResponder.new
66
+
67
+ responder.hello #=> "hi"
68
+ fake.hello #=> nil
69
+
70
+ responder.name #=> "John"
71
+ fake.name #=> "None"
72
+
73
+ responder.full_name "Jane", "Doe" #=> "Jane Doe"
74
+ fake.full_name "Avdi", "Burnhart" #=> nil
75
+
76
+ NameResponder.reverse "Ryan" #=> "nayR"
77
+ FakeResponder.reverse "Ryan" #=> "no name"
78
+
79
+ ````
80
+
81
+ ## Error Handling ##
82
+ To keep you from stubbing methods that don't exist and thus getting your fake out of sync with your base class, Mimic throws two custom exceptions based on what travesty you committed.
83
+
84
+ If you try to stub an instance method that does not belong on the base class, you will get `InstanceMethodNotDefined` Error.
85
+
86
+ If you try to stub a class method that does not belong on the base class, you will get `ClassMethodNotDefined` Error.
87
+
88
+ Pretty self explanatory. These are simply here as a gentle reminder that will help keep your fake in sync with your base class before you get in production and start chasing nil and NoMethod errors.
89
+
90
+
91
+
92
+ ## Developing for Mimic ##
93
+ If you have any feature requests or bug fixes, I actively encourage you to try out integrating those changes yourself and making a pull request.
94
+
95
+ First fork the repo
96
+ ````sh
97
+ git clone [GITHUB GIT URL]
98
+ bundle
99
+ rspec
100
+ ````
101
+ Check all the tests and then from there have fun!
102
+
103
+
104
+ ## TODO ##
105
+
106
+ * Test that setters and getters are also properly mocked/stubbed
107
+ * See if it'd be a horrible idea to mix this into ruby's class so you don't have to `include Mimic` in your mimic classes
108
+ * Module Stubbing/Mocking
109
+ * Strict Parameter checking. **Currently all mocked methods allow n arguments.**
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
data/lib/mimic.rb ADDED
@@ -0,0 +1,80 @@
1
+ class MethodNotDefined < StandardError
2
+ end
3
+
4
+ class InstanceMethodNotDefined < MethodNotDefined
5
+ end
6
+
7
+ class ClassMethodNotDefined < MethodNotDefined
8
+ end
9
+
10
+ module Mimic
11
+
12
+ def self.included(base)
13
+ base.extend(ClassMethods)
14
+ end
15
+
16
+ module ClassMethods
17
+ #To have a file strictly adhere to the implementation of another file, call <tt>mimics</tt> in
18
+ #any class followed by the class name as an attribute.
19
+ #
20
+ # class NullUser
21
+ # include Mimic
22
+ # mimics User
23
+ # end
24
+ #
25
+ #This will add all the class and instance level methods to your class that are defined in the class
26
+ #being mimicked. All methods will default to returning nil when called. Please keep this in mind.
27
+ def mimics(class_name)
28
+ class_name.instance_methods(false).map do |method_name|
29
+ params = class_name.instance_method(method_name).parameters.map(&:last)
30
+ define_method(method_name) do |*args|
31
+ raise ArgumentError unless args.count.eql? params.count
32
+ end
33
+ end
34
+ class_name.methods(false).map do |method_name|
35
+ params = class_name.method(method_name).parameters.map(&:last)
36
+ define_singleton_method(method_name) do |*args|
37
+ raise ArgumentError unless args.count.eql? params.count
38
+ end
39
+ end
40
+ end
41
+
42
+ #If you want to override the default behavior of returning nil for methods on the original class
43
+ #simply call <tt>stubs</tt> followed by the method name and a block with the return value desired.
44
+ #
45
+ # class NullUser
46
+ # include mimic
47
+ # mimics User
48
+ #
49
+ # stubs(:name) { "No Name" }
50
+ # end
51
+ #
52
+ def stubs(method_name, &block)
53
+ raise InstanceMethodNotDefined, error_msg(method_name) unless method_defined?(method_name)
54
+ define_method(method_name) { |*args| yield }
55
+ end
56
+
57
+ #If you want to override the default behavior of returning nil for class methods on the original class
58
+ #simply call <tt>class_stubs</tt> followed by the method name and a block with the return value desired.
59
+ #
60
+ # class NullUser
61
+ # include mimic
62
+ # mimics User
63
+ #
64
+ # class_stubs(:age_for) { 10 }
65
+ # end
66
+ #
67
+ def class_stubs(method_name, &block)
68
+ raise ClassMethodNotDefined, error_msg(method_name) unless methods(false).include?(method_name)
69
+ define_singleton_method(method_name) { |*args| yield }
70
+ end
71
+
72
+ def arg_error_msg(args, params)
73
+ "Wrong Number of Arguements for Method. (#{args.count}) of (#{params.count})"
74
+ end
75
+
76
+ def error_msg(method_name)
77
+ "Method: #{method_name} Isn't Defined on Base Class"
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,3 @@
1
+ module Mimic
2
+ VERSION = "0.1.1"
3
+ end
data/mimic-rb.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/mimic/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Justin Herrick"]
6
+ gem.email = ["justin@justinherrick.com"]
7
+ gem.description = %q{Minimalistic Library for Mocking, Stubbing, and Faking Classes}
8
+ gem.summary = %q{Rspec Style Mocking and Stubbing for creating Null Objects and Faking Classes with error handling to keep your Fake's in sync}
9
+ gem.homepage = "https://github.com/jah2488/mimic"
10
+
11
+ gem.add_development_dependency "rake"
12
+ gem.add_development_dependency "rspec"
13
+ gem.files = `git ls-files`.split($\)
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.name = "mimic-rb"
17
+ gem.require_paths = ["lib"]
18
+ gem.version = Mimic::VERSION
19
+ end
@@ -0,0 +1,93 @@
1
+ require 'mimic'
2
+
3
+ class MyNode
4
+ def hello
5
+ "hi"
6
+ end
7
+ def johnny
8
+ "bill"
9
+ end
10
+ def add(first, second)
11
+ first + second
12
+ end
13
+ def self.used
14
+ "u called me"
15
+ end
16
+ def self.holla
17
+ "back"
18
+ end
19
+ end
20
+
21
+ class MyFakeNode
22
+ include Mimic
23
+ mimics MyNode
24
+ stubs(:add) { "five" }
25
+ class_stubs(:used) { 1000 }
26
+ end
27
+
28
+
29
+ describe Mimic do
30
+
31
+ it "mocks all methods on fake class" do
32
+ MyFakeNode.instance_methods(false).should == MyNode.instance_methods(false)
33
+ end
34
+ it "mocks all class methods on fake class" do
35
+ MyFakeNode.methods(false).should == MyNode.methods(false)
36
+ end
37
+
38
+ it "returns nil for instance method calls" do
39
+ a = MyFakeNode.new
40
+ a.hello.should be_nil
41
+ a.johnny.should be_nil
42
+ end
43
+
44
+ it "returns nil for class method calls" do
45
+ MyFakeNode.holla.should be_nil
46
+ end
47
+
48
+ it "should let you stub a class method" do
49
+ MyFakeNode.used.should == 1000
50
+ end
51
+
52
+ it "returns the stubbed response for a given methods" do
53
+ a = MyFakeNode.new
54
+ a.add.should == "five"
55
+ end
56
+
57
+ it "will return the stubbed response even when the method is given parameters" do
58
+ a = MyFakeNode.new
59
+ a.add(2,5).should == "five"
60
+ end
61
+
62
+ it "should raise exception if method is not defined on base class" do
63
+ lambda {
64
+ a = class MyNullObject
65
+ include Mimic
66
+ mimics MyNode
67
+ stubs(:subtract) { "10"}
68
+ end
69
+ }.should raise_error InstanceMethodNotDefined
70
+ end
71
+
72
+ it "should raise exception if method is not defined on base class" do
73
+ lambda {
74
+ a = class MyNullObject
75
+ include Mimic
76
+ mimics MyNode
77
+ class_stubs(:subtract) { "10" }
78
+ end
79
+ }.should raise_error ClassMethodNotDefined
80
+ end
81
+
82
+ it "should raise exceptions on methods being called with wrong amount of arguements" do
83
+ lambda {
84
+ class MyNullObject
85
+ include Mimic
86
+ mimics MyNode
87
+ end
88
+ a = MyNullObject.new
89
+ a.hello(10)
90
+ }.should raise_error ArgumentError
91
+ end
92
+
93
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mimic-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Justin Herrick
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Minimalistic Library for Mocking, Stubbing, and Faking Classes
47
+ email:
48
+ - justin@justinherrick.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - .travis.yml
56
+ - CHANGELOG.md
57
+ - Gemfile
58
+ - Gemfile.lock
59
+ - Guardfile
60
+ - LICENSE
61
+ - README.md
62
+ - Rakefile
63
+ - lib/mimic.rb
64
+ - lib/mimic/version.rb
65
+ - mimic-rb.gemspec
66
+ - spec/mimic_spec.rb
67
+ homepage: https://github.com/jah2488/mimic
68
+ licenses: []
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 1.8.22
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: Rspec Style Mocking and Stubbing for creating Null Objects and Faking Classes
91
+ with error handling to keep your Fake's in sync
92
+ test_files:
93
+ - spec/mimic_spec.rb
94
+ has_rdoc: