sham 0.5.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +2 -0
- data/License.txt +1 -1
- data/README.markdown +115 -56
- data/lib/sham.rb +55 -19
- data/lib/sham/version.rb +1 -1
- data/sham.gemspec +1 -0
- metadata +34 -32
data/Gemfile
CHANGED
data/License.txt
CHANGED
data/README.markdown
CHANGED
@@ -10,96 +10,155 @@ Lightweight flexible factories for Ruby on Rails testing.
|
|
10
10
|
|
11
11
|
Create a sham file for each of your models:
|
12
12
|
|
13
|
-
# in sham/
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
13
|
+
# in sham/user.rb
|
14
|
+
Sham.config(User) do |c|
|
15
|
+
c.attributes do
|
16
|
+
{ :name => "Sample User" }
|
17
|
+
end
|
18
18
|
end
|
19
19
|
|
20
|
-
To
|
20
|
+
To load your shams you can either include the files individually, or define
|
21
|
+
your shams directly in your test file. Sham also provides a helper function to
|
22
|
+
load shams under the sham directory. To load all your shams add the following to
|
23
|
+
your application.rb or test.rb file:
|
21
24
|
|
22
25
|
config.after_initialize do
|
23
|
-
|
26
|
+
Sham::Config.activate!
|
24
27
|
end
|
25
|
-
|
26
|
-
To enable
|
28
|
+
|
29
|
+
To enable all Shams in cucumber, add the following to your
|
30
|
+
features/support/env.rb file:
|
27
31
|
|
28
32
|
require 'sham'
|
29
|
-
Sham::Config.activate!
|
33
|
+
Sham::Config.activate!
|
30
34
|
|
31
35
|
You can now "sham" your models and pass additional attributes at creation:
|
32
36
|
|
33
37
|
User.sham!
|
34
|
-
User.sham!
|
35
|
-
User.sham!
|
36
|
-
|
37
|
-
You can use sham to build models without saving them as well:
|
38
|
+
User.sham!(:name => "New Name")
|
39
|
+
User.sham!(:age => 23)
|
40
|
+
|
41
|
+
You can use sham to build models without automatically saving them as well:
|
38
42
|
|
39
|
-
user = User.sham!
|
43
|
+
user = User.sham!(:build, :name => "I have not been saved")
|
40
44
|
user.save
|
41
|
-
|
45
|
+
|
42
46
|
## RSpec Example
|
43
47
|
|
44
|
-
Here is an example of testing validations on an ActiveRecord::Base class using
|
48
|
+
Here is an example of testing validations on an ActiveRecord::Base class using
|
49
|
+
Sham and RSpec.
|
45
50
|
|
46
51
|
# in app/models/item.rb
|
47
52
|
class Item < ActiveRecord::Base
|
48
|
-
|
53
|
+
validates_numericality_of :quantity, :greater_than => 0
|
49
54
|
end
|
50
55
|
|
51
|
-
# in sham/
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
+
# in sham/item.rb
|
57
|
+
Sham.config(Item) do |c|
|
58
|
+
c.attributes do
|
59
|
+
{ :quantity => 1 }
|
60
|
+
end
|
56
61
|
end
|
57
62
|
|
58
63
|
# in spec/models/item_spec.rb
|
64
|
+
require 'spec_helper'
|
65
|
+
require './sham/item'
|
66
|
+
|
59
67
|
describe Item do
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
68
|
+
it "should not allow items with a negative price" do
|
69
|
+
item = Item.sham!(:build, :quantity => -1)
|
70
|
+
item.valid?.should be_false
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should allow items with a positive quantity" do
|
74
|
+
item = Item.sham!(:build, :quantity => 10)
|
75
|
+
item.valid?.should be_true
|
76
|
+
end
|
69
77
|
end
|
70
|
-
|
71
|
-
##
|
72
|
-
|
73
|
-
You can
|
74
|
-
|
75
|
-
# in sham/
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
def self.large_options
|
82
|
-
{ :weight => 100.0 }
|
83
|
-
end
|
78
|
+
|
79
|
+
## Alternative Shams
|
80
|
+
|
81
|
+
You can easily define alternative sham configurations:
|
82
|
+
|
83
|
+
# in sham/item.rb
|
84
|
+
Sham.config(Item, :small) do |c|
|
85
|
+
c.attributes do
|
86
|
+
{ :weight => 10.0 }
|
87
|
+
end
|
84
88
|
end
|
85
|
-
|
89
|
+
|
90
|
+
Sham.config(Item, :large) do |c|
|
91
|
+
c.attributes do
|
92
|
+
{ :weight => 100.0 }
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
86
96
|
These can be invoked using:
|
87
97
|
|
88
|
-
Item.
|
89
|
-
Item.
|
90
|
-
|
98
|
+
Item.sham!(:small, :quantity => 100)
|
99
|
+
Item.sham!(:large, :build, :quantity => 0)
|
100
|
+
|
101
|
+
## Empty Shams
|
102
|
+
|
103
|
+
You can easily define empty shams using the empty function:
|
104
|
+
|
105
|
+
# in sham/user.rb
|
106
|
+
Sham.config(User) do |c|
|
107
|
+
c.empty
|
108
|
+
end
|
109
|
+
|
110
|
+
This can be invoked using:
|
111
|
+
|
112
|
+
User.sham!
|
113
|
+
|
91
114
|
## Nested Shamming
|
92
115
|
|
93
116
|
You can nest shammed models inside others:
|
94
117
|
|
95
118
|
# in sham/line_item_sham.rb
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
119
|
+
Sham.config(LineItem) do |c|
|
120
|
+
c.attributes do
|
121
|
+
{ :item => Sham::Base.new(Item) }
|
122
|
+
end
|
100
123
|
end
|
101
124
|
|
102
|
-
The nested shams will automatically be invoked and can be overridden during a
|
125
|
+
The nested shams will automatically be invoked and can be overridden during a
|
126
|
+
sham call:
|
103
127
|
|
104
128
|
LineItem.sham!
|
105
|
-
LineItem.sham!
|
129
|
+
LineItem.sham!(:item => Item.sham!(:weight => 100))
|
130
|
+
|
131
|
+
|
132
|
+
## Subclass Shams
|
133
|
+
|
134
|
+
Sham plays well with subclassing. That means shams defined on parent classes
|
135
|
+
will be available to child classes as well:
|
136
|
+
|
137
|
+
Sham.config(Person) do |c|
|
138
|
+
c.empty
|
139
|
+
end
|
140
|
+
|
141
|
+
class Person; end
|
142
|
+
class Employee < Person; end
|
143
|
+
|
144
|
+
Employee.sham!
|
145
|
+
|
146
|
+
## Reloading Shams with Spork
|
147
|
+
|
148
|
+
[Spork](https://rubygems.org/gems/spork) is a great gem that creates a
|
149
|
+
Distributed Ruby environment that you can run your RSpec and Cucumber tests
|
150
|
+
against. If you are using Rails it is often necessary to re-load your models and
|
151
|
+
controllers between Spork test runs so that the Spork DRB picks up your latest
|
152
|
+
model changes. This is usually accomplished using a Spork 'each run' block. This
|
153
|
+
block of code gets executed before each test run. If you want to be able to
|
154
|
+
reload your shams with Spork all you need to do is add a Sham::Config.activate!
|
155
|
+
line to this block after you have re-loaded your models and controllers.
|
156
|
+
|
157
|
+
Spork.each_run do
|
158
|
+
ActiveSupport::Dependencies.clear
|
159
|
+
ActiveRecord::Base.instantiate_observers
|
160
|
+
Sham::Config.activate!
|
161
|
+
end if Spork.using_spork?
|
162
|
+
|
163
|
+
This change will cause sham to be re-loaded so that you can continue to use it
|
164
|
+
with Spork.
|
data/lib/sham.rb
CHANGED
@@ -14,11 +14,32 @@ module Sham
|
|
14
14
|
|
15
15
|
class Config
|
16
16
|
def self.activate!
|
17
|
-
Dir["#{Rails.root}/sham
|
17
|
+
Dir["#{Rails.root}/sham/**/*.rb"].each do |f|
|
18
18
|
load f
|
19
|
-
(File.basename(f).match(/(.*)_sham.rb/)[1]).classify.constantize.send :include, Sham::Methods
|
20
19
|
end
|
21
20
|
end
|
21
|
+
|
22
|
+
attr_accessor :klass, :name
|
23
|
+
|
24
|
+
def initialize klass, name
|
25
|
+
self.klass = klass
|
26
|
+
self.name = name
|
27
|
+
end
|
28
|
+
|
29
|
+
def attributes &attributes
|
30
|
+
klass.add_sham_attributes(name, attributes)
|
31
|
+
end
|
32
|
+
|
33
|
+
def empty
|
34
|
+
klass.add_sham_attributes(name, Proc.new{ Hash.new() })
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.config klass, name = :default
|
39
|
+
unless klass.include?(Sham::Shammable)
|
40
|
+
klass.send(:include, Sham::Shammable)
|
41
|
+
end
|
42
|
+
yield(Sham::Config.new(klass, name))
|
22
43
|
end
|
23
44
|
|
24
45
|
def self.string!
|
@@ -37,31 +58,46 @@ module Sham
|
|
37
58
|
end
|
38
59
|
end
|
39
60
|
|
40
|
-
def self.add_options! klass, options = {},
|
41
|
-
|
61
|
+
def self.add_options! klass, options = {}, attributes
|
62
|
+
attributes.call.each do |key, value|
|
42
63
|
options[key] = self.parse!(value) unless options.has_key?(key)
|
43
64
|
end
|
44
65
|
end
|
45
66
|
|
46
|
-
module
|
47
|
-
def self.included
|
48
|
-
klass.
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
67
|
+
module Shammable
|
68
|
+
def self.included(klass)
|
69
|
+
klass.extend(ClassMethods)
|
70
|
+
|
71
|
+
klass.class_eval <<-EVAL
|
72
|
+
def self.add_sham_attributes name, attributes
|
73
|
+
@@sham_attributes ||= {}
|
74
|
+
@@sham_attributes[name] = attributes
|
75
|
+
end
|
76
|
+
|
77
|
+
def self.sham_attributes
|
78
|
+
@@sham_attributes
|
55
79
|
end
|
80
|
+
EVAL
|
81
|
+
end
|
82
|
+
|
83
|
+
module ClassMethods
|
84
|
+
def sham! *args
|
85
|
+
options = (args.extract_options! || {})
|
86
|
+
type = (args[0] == :build ? args[1] : args[0]) || :default
|
87
|
+
build = args[0] == :build || args[1] == :build
|
56
88
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
klass
|
61
|
-
|
62
|
-
|
89
|
+
::Sham.add_options! self.name, options, sham_attributes[type]
|
90
|
+
klass = (options.delete(:type) || self.name).constantize
|
91
|
+
if build
|
92
|
+
klass.new(options)
|
93
|
+
else
|
94
|
+
klass.create(options)
|
63
95
|
end
|
64
96
|
end
|
97
|
+
|
98
|
+
def sham_alternate! type, *args
|
99
|
+
sham!(type, *args)
|
100
|
+
end
|
65
101
|
end
|
66
102
|
end
|
67
103
|
end
|
data/lib/sham/version.rb
CHANGED
data/sham.gemspec
CHANGED
metadata
CHANGED
@@ -1,30 +1,36 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: sham
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
4
5
|
prerelease:
|
5
|
-
version: 0.5.0
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Pan Thomakos
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
date: 2011-09-13 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: &70123949484060 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.1
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70123949484060
|
17
25
|
description: Lightweight flexible factories for Ruby on Rails testing.
|
18
|
-
email:
|
26
|
+
email:
|
19
27
|
- pan.thomakos@gmail.com
|
20
28
|
executables: []
|
21
|
-
|
22
29
|
extensions: []
|
23
|
-
|
24
|
-
extra_rdoc_files:
|
30
|
+
extra_rdoc_files:
|
25
31
|
- README.markdown
|
26
32
|
- License.txt
|
27
|
-
files:
|
33
|
+
files:
|
28
34
|
- .document
|
29
35
|
- .gitignore
|
30
36
|
- Gemfile
|
@@ -34,33 +40,29 @@ files:
|
|
34
40
|
- lib/sham.rb
|
35
41
|
- lib/sham/version.rb
|
36
42
|
- sham.gemspec
|
37
|
-
has_rdoc: true
|
38
43
|
homepage: http://github.com/panthomakos/sham
|
39
44
|
licenses: []
|
40
|
-
|
41
45
|
post_install_message:
|
42
|
-
rdoc_options:
|
46
|
+
rdoc_options:
|
43
47
|
- --charset=UTF-8
|
44
|
-
require_paths:
|
48
|
+
require_paths:
|
45
49
|
- lib
|
46
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
51
|
none: false
|
48
|
-
requirements:
|
49
|
-
- -
|
50
|
-
- !ruby/object:Gem::Version
|
51
|
-
version:
|
52
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
57
|
none: false
|
54
|
-
requirements:
|
55
|
-
- -
|
56
|
-
- !ruby/object:Gem::Version
|
57
|
-
version:
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
58
62
|
requirements: []
|
59
|
-
|
60
63
|
rubyforge_project: sham
|
61
|
-
rubygems_version: 1.6
|
64
|
+
rubygems_version: 1.8.6
|
62
65
|
signing_key:
|
63
66
|
specification_version: 3
|
64
|
-
summary: sham-0.
|
67
|
+
summary: sham-1.0.0
|
65
68
|
test_files: []
|
66
|
-
|