spoof 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +22 -0
  3. data/README.md +115 -0
  4. data/lib/spoof/version.rb +3 -0
  5. data/lib/spoof.rb +49 -0
  6. metadata +91 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 552c0407bc0a7ae55df4dd59436279c2ddd4d493
4
+ data.tar.gz: d1ec0fa54a12f11c033cf6bf85fad95c41e19ac7
5
+ SHA512:
6
+ metadata.gz: 3a73457f5f0d5118b0e9a9ab3535eff774f6db3528ef59bf2eef44d77267ee9756d6eaf76a29850b79bb8a22209c97bc0336dee1c6e9dfbbe882e06c83770df8
7
+ data.tar.gz: 50a46ea25925946b57b755e932501f32a319df89097cac640885408334b1b429b9692347f0cc6e2b0e05c41f03ae9977a7589e731440762bfc4d0e292b4ebf0d
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Nathan Hopkins
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,115 @@
1
+ [![Lines of Code](http://img.shields.io/badge/lines_of_code-35-brightgreen.svg?style=flat)](http://blog.codinghorror.com/the-best-code-is-no-code-at-all/)
2
+ [![Code Status](http://img.shields.io/codeclimate/github/hopsoft/spoof.svg?style=flat)](https://codeclimate.com/github/hopsoft/spoof)
3
+ [![Dependency Status](http://img.shields.io/gemnasium/hopsoft/spoof.svg?style=flat)](https://gemnasium.com/hopsoft/spoof)
4
+ [![Build Status](http://img.shields.io/travis/hopsoft/spoof.svg?style=flat)](https://travis-ci.org/hopsoft/spoof)
5
+ [![Coverage Status](https://img.shields.io/coveralls/hopsoft/spoof.svg?style=flat)](https://coveralls.io/r/hopsoft/spoof?branch=master)
6
+ [![Downloads](http://img.shields.io/gem/dt/spoof.svg?style=flat)](http://rubygems.org/gems/spoof)
7
+
8
+ # Spoof
9
+
10
+ A small "mocking framework" to help you write more effective tests.
11
+
12
+ *Also, checkout [pry-test](https://github.com/hopsoft/pry-test) for a lightweight test framework.*
13
+
14
+ ## Quick Start
15
+
16
+ ```bash
17
+ gem install spoof
18
+ ```
19
+
20
+ ```ruby
21
+ # create a mock class
22
+ MyMock = Spoof.make
23
+
24
+ # add a class attr
25
+ MyMock.attr(:foo)
26
+
27
+ # add several class attrs at once
28
+ mock.attrs(:one, :two, :three)
29
+
30
+ # add a class attr with a default value
31
+ MyMock.attr(:attr_with_default, "Class value")
32
+
33
+ # add a class method
34
+ MyMock.method(:say_foo) { |arg| "#{foo} #{arg}!" }
35
+
36
+ # create a mock instance
37
+ mock = MyMock.new
38
+
39
+ # add an instance attr
40
+ mock.attr(:bar)
41
+
42
+ # add several instance attrs at once
43
+ mock.attrs(:first, :second, :third)
44
+
45
+ # add an instance attr with a default value
46
+ mock.attr(:attr_with_default, "Instance value")
47
+
48
+ # add an instance method
49
+ mock.method(:say_bar) { |arg| "#{bar} #{arg}!" }
50
+
51
+ # use the mock
52
+ MyMock.attr_with_default # => "Class value"
53
+ MyMock.foo # => nil
54
+ MyMock.foo = :foo
55
+ MyMock.say_foo :bar # => "foo bar!"
56
+
57
+ mock.attr_with_default # => "Instance value"
58
+ mock.bar # => nil
59
+ mock.bar = :bar
60
+ mock.say_bar :foo # => "bar foo!"
61
+ ```
62
+
63
+ ## Next Steps
64
+
65
+ ```ruby
66
+ # create a useless module to illustrate mocking with ancestors
67
+ module Useless
68
+ def reverse_string
69
+ reverse.join(",")
70
+ end
71
+ end
72
+
73
+ # create a mock that subclasses Array and mixes in the Useless module defined above
74
+ # note: the superclass must be passed before mixin modules
75
+ MockList = Spoof.make(Array, Useless)
76
+
77
+ list = MockList.new
78
+
79
+ # demonstrate that the mock has inherited behavior
80
+ list.concat [1, 2, 3]
81
+ list.reverse_string # => "3,2,1"
82
+
83
+ # add an instance method that does something interesting
84
+ list.method :prefixed do |prefix|
85
+ map { |value| "#{prefix}:#{value}"}
86
+ end
87
+ list.prefixed(:num) # => ["num:1", "num:2", "num:3"]
88
+ ```
89
+
90
+ ## Deep Cuts
91
+
92
+ Here is an example that mocks part of ActiveRecord.
93
+
94
+ ```ruby
95
+ Model = Spoof.make
96
+ Model.method(:find) { |*args| model.clone }
97
+ Model.method(:all) { (1..5).map { model.clone } }
98
+
99
+ model = Model.new
100
+ model.method(:destroy) { @destroyed = true }
101
+ model.method(:destroyed?) { @destroyed }
102
+ model.method(:update_attributes) { |*args| @attributes_updated = true }
103
+ model.method(:save) { |*args| @saved = true }
104
+
105
+ # try it out
106
+ list = Model.all # => [#<Spoof70331390241500:0x007fee9b1b1bb0 @args=[]>, #<Spoof...]
107
+ m = Model.find(1) # => #<Spoof70331390241500:0x007fee9b17b6a0 @args=[]>
108
+ m.update_attributes(:foo, "bar") # => true
109
+ m.save # => true
110
+ m.destroy # => true
111
+ m.destroyed? # => true
112
+ ```
113
+
114
+ For a more complete example, check out [Coast's test suite](https://github.com/hopsoft/coast/blob/master/test/test_coast.rb) which mocks a significant portion of Rails.
115
+
@@ -0,0 +1,3 @@
1
+ module Spoof
2
+ VERSION = "2.0.0"
3
+ end
data/lib/spoof.rb ADDED
@@ -0,0 +1,49 @@
1
+ # Spoof is a tiny mocking script.
2
+ module Spoof
3
+
4
+ # Defines a mock class.
5
+ def self.make(*ancestors)
6
+ superclass = ancestors.shift if ancestors.first.is_a?(Class)
7
+ superclass ||= Object
8
+ mixins = ancestors
9
+ klass = Class.new(superclass) do
10
+ mixins.each { |mixin| send :include, mixin }
11
+ def initialize(*args)
12
+ @args = args
13
+ super unless self.class.superclass == Object
14
+ end
15
+ end
16
+ klass.extend Spoof
17
+ klass.send :include, Spoof
18
+ klass
19
+ end
20
+
21
+ # Creates an attribute getter & setter.
22
+ # @param [Symbol] name The name of the attribute.
23
+ # @param [Object] default_value An optional default value.
24
+ def attr(name, default_value=nil)
25
+ context.send :attr_accessor, name
26
+ instance_variable_set "@#{name}", default_value
27
+ end
28
+
29
+ # Creates attributes for a list of names.
30
+ # @param [Symbol] names The list of attribute names.
31
+ def attrs(*names)
32
+ names.each { |name| attr(name) }
33
+ end
34
+
35
+ # Creates a method.
36
+ # @param [Symbol] name The name of the method.
37
+ # @yield The block that will serve as the method body.
38
+ def method(name, &block)
39
+ context.send :define_method, name, &block
40
+ end
41
+
42
+ private
43
+
44
+ def context
45
+ @context = class << self; self; end if is_a? Class
46
+ @context ||= self.class
47
+ end
48
+
49
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spoof
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Hopkins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry-test
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: coveralls
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: A small "mocking framework" to help you write more effective tests.
56
+ email:
57
+ - natehop@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE.txt
63
+ - README.md
64
+ - lib/spoof.rb
65
+ - lib/spoof/version.rb
66
+ homepage: http://hopsoft.github.com/spoof/
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.2.2
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: A small "mocking framework" to help you write more effective tests.
90
+ test_files: []
91
+ has_rdoc: