yockeries 0.0.1
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.
- data/.gitignore +17 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +22 -0
- data/README.rdoc +62 -0
- data/Rakefile +5 -0
- data/lib/yockeries.rb +65 -0
- data/test/yockeries_test.rb +33 -0
- data/yockeries.gemspec +18 -0
- metadata +56 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Robert Evans
|
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.rdoc
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
= Yockeries - Yaml + Mocks + Factories(easy way to create them)
|
2
|
+
|
3
|
+
Long ago there was an uprising against YAML fixtures in the rails world. Some
|
4
|
+
people replaced them with things like FactoryGirl, Machinist, Fabrication, and a
|
5
|
+
bunch of others.
|
6
|
+
|
7
|
+
However, YAML is still the simpliest thing that works, because it comes with
|
8
|
+
Ruby. When you load in a yaml file with YAML.load_file, the yaml data is
|
9
|
+
converted into a hash, which again is simple to use and test. You can take
|
10
|
+
that hash and create a new object with it: Person.new(yaml_converted_hash),
|
11
|
+
you can use it to create a stand-in double/mock:
|
12
|
+
OpenStruct.new(yaml_converted_hash).
|
13
|
+
|
14
|
+
+Yockeries+ is meant to make that a tiny bit simplier with a
|
15
|
+
small amount of code.
|
16
|
+
|
17
|
+
+Yockeries+ gives a convienent yaml loader (smart enough to know if you are
|
18
|
+
using rspec, minitest, or testunit) to load an entire fixture file into a hash
|
19
|
+
and then use the +get+ method to select which yaml object you want to work
|
20
|
+
with.
|
21
|
+
|
22
|
+
There is also a method +mock_for+ that will create an +OpenStruct+ object where
|
23
|
+
you can create a very simple test +double/mock+.
|
24
|
+
|
25
|
+
|
26
|
+
=== YAML File (people.yaml)
|
27
|
+
|
28
|
+
robert:
|
29
|
+
name: Robert Evans
|
30
|
+
email: robert@example.com
|
31
|
+
username: revans
|
32
|
+
password: 123456
|
33
|
+
password_confirmation: 123456
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
== Examples:
|
38
|
+
|
39
|
+
=== Load a specific User from a YAML File
|
40
|
+
|
41
|
+
fixture(:people).get(:robert) # => {:name=>"Robert Evans", :email=>"robert@example.com", :username=>"revans", :password=>123456, :password_confirmation=>123456}
|
42
|
+
|
43
|
+
|
44
|
+
=== Load a mock Object from the YAML file
|
45
|
+
|
46
|
+
fixture(:people).mock_for(:robert) #=> #<OpenStruct name="Robert Evans", email="robert@example.com", username="revans", password=123456, password_confirmation=123456>
|
47
|
+
|
48
|
+
=== Real world example with testunit:
|
49
|
+
|
50
|
+
assert_difference "Person.count", 1 do
|
51
|
+
Person.new(
|
52
|
+
fixture(:people).get(:robert)
|
53
|
+
)
|
54
|
+
end
|
55
|
+
|
56
|
+
=== Real world example with rspec
|
57
|
+
|
58
|
+
expect {
|
59
|
+
Person.new(
|
60
|
+
fixture(:people).get(:robert)
|
61
|
+
).save
|
62
|
+
}.to change { Person.count }.by(1)
|
data/Rakefile
ADDED
data/lib/yockeries.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
module Yockeries
|
5
|
+
class YockHash < Hash
|
6
|
+
def initialize(hash = {})
|
7
|
+
@hash = symbolize_keys(hash)
|
8
|
+
end
|
9
|
+
|
10
|
+
def get(name)
|
11
|
+
@hash[name.to_sym]
|
12
|
+
end
|
13
|
+
|
14
|
+
def mock_for(name)
|
15
|
+
OpenStruct.new(get(name))
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def symbolize_keys(hash)
|
21
|
+
case hash
|
22
|
+
when Array
|
23
|
+
symbolize_array_keys(hash)
|
24
|
+
when Hash
|
25
|
+
symbolize_hash_keys(hash)
|
26
|
+
else
|
27
|
+
hash
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def symbolize_array_keys(array)
|
32
|
+
array.inject([]) do |result, value|
|
33
|
+
result << case value
|
34
|
+
when Hash, Array
|
35
|
+
symbolize_keys(value)
|
36
|
+
else
|
37
|
+
value
|
38
|
+
end
|
39
|
+
result
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def symbolize_hash_keys(hash)
|
44
|
+
hash.inject({}) do |result, (key,value)|
|
45
|
+
nval = case value
|
46
|
+
when Hash, Array
|
47
|
+
symbolize_keys(value)
|
48
|
+
else
|
49
|
+
value
|
50
|
+
end
|
51
|
+
result[key.to_sym] = nval
|
52
|
+
result
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
module Loader
|
60
|
+
def fixture(filename)
|
61
|
+
dir = File.exists?('test') ? 'test' : 'spec'
|
62
|
+
::Yockeries::YockHash.new(YAML.load_file("#{dir}/fixtures/#{filename.to_s}.yaml"))
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require_relative '../lib/yockeries'
|
3
|
+
|
4
|
+
module Yockeries
|
5
|
+
describe YockHash do
|
6
|
+
let(:hash) do
|
7
|
+
{ 'user_1' => { 'name' => 'robert', 'email' => 'robert@example.com' },
|
8
|
+
'user_2' => { 'name' => 'tyler', 'email' => 'ty@example.com' } }
|
9
|
+
end
|
10
|
+
|
11
|
+
subject { YockHash.new(hash) }
|
12
|
+
|
13
|
+
it 'symbolizes the keys' do
|
14
|
+
subject.get('user_1').key?(:name).must_equal true
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'will return the name for user_1' do
|
18
|
+
subject.get('user_1')[:name].must_equal 'robert'
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'will return the name for user_2' do
|
22
|
+
subject.get('user_2')[:name].must_equal 'tyler'
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'will return a mock' do
|
26
|
+
subject.mock_for('user_1').kind_of?(OpenStruct).must_equal true
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'mocks will imitate an object' do
|
30
|
+
subject.mock_for('user_1').name.must_equal 'robert'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/yockeries.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: 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 |gem|
|
6
|
+
gem.name = "yockeries"
|
7
|
+
gem.version = '0.0.1'
|
8
|
+
gem.authors = ["Robert Evans"]
|
9
|
+
gem.email = ["robert@codewranglers.org"]
|
10
|
+
gem.description = %q{Use Ruby Fixtures to create mocks or use in place of factories to create objects}
|
11
|
+
gem.summary = %q{Use Ruby Fixtures to create mocks or use in place of factories to create objects}
|
12
|
+
gem.homepage = ""
|
13
|
+
|
14
|
+
gem.files = `git ls-files`.split($/)
|
15
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
16
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
17
|
+
gem.require_paths = ["lib"]
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yockeries
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Robert Evans
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-04 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Use Ruby Fixtures to create mocks or use in place of factories to create
|
15
|
+
objects
|
16
|
+
email:
|
17
|
+
- robert@codewranglers.org
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.rdoc
|
26
|
+
- Rakefile
|
27
|
+
- lib/yockeries.rb
|
28
|
+
- test/yockeries_test.rb
|
29
|
+
- yockeries.gemspec
|
30
|
+
homepage: ''
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.8.23
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: Use Ruby Fixtures to create mocks or use in place of factories to create
|
54
|
+
objects
|
55
|
+
test_files:
|
56
|
+
- test/yockeries_test.rb
|