modelfactory 0.7.0 → 0.8.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.
- data/History.txt +4 -0
- data/README.txt +42 -4
- data/lib/model_factory.rb +8 -6
- data/lib/model_factory/version.rb +1 -1
- metadata +3 -3
data/History.txt
CHANGED
data/README.txt
CHANGED
@@ -3,6 +3,9 @@
|
|
3
3
|
ModelFactory is a module designed to replace the use of fixtures for testing
|
4
4
|
Rails applications.
|
5
5
|
|
6
|
+
The best explanation for the motivation behind ModelFactory (and the inspiration
|
7
|
+
for this module) comes from Dan Manges' blog: http://www.dcmanges.com/blog/38
|
8
|
+
|
6
9
|
The idea is that instead of keeping your test data in a nearly opaque fixture
|
7
10
|
file, you generate data in the test itself using a custom factory API designed
|
8
11
|
for your test environment.
|
@@ -16,13 +19,18 @@ the database.
|
|
16
19
|
|
17
20
|
=== A Note About Defaults
|
18
21
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
+
The purpose of default values is to generate valid instances, not to serve as
|
23
|
+
replacements for fixture data. When writing tests that use factory-generated
|
24
|
+
objects, it's important never to depend on default values in your test assertions.
|
25
|
+
If you depend on defaults in your tests they become more fragile and the intention
|
26
|
+
is harder to discern. Alway override values you care about when using factory objects.
|
22
27
|
|
23
28
|
If you find yourself repeating the same initialization to avoid using defaults,
|
24
29
|
consider whether it would be appropriate to add a custom toplevel method to
|
25
|
-
your factory module that includes this initialization.
|
30
|
+
your factory module that includes this initialization. You can also specify
|
31
|
+
multiple named types of defaults, described below. Be aware that both of these
|
32
|
+
techniques should be used sparingly, as they can have some of the same issues
|
33
|
+
as fixtures.
|
26
34
|
|
27
35
|
=== A Note About ID Generation
|
28
36
|
|
@@ -80,6 +88,36 @@ Then in your tests you use Factory methods to instantiate your test objects:
|
|
80
88
|
assert !user.likes_blue?
|
81
89
|
end
|
82
90
|
|
91
|
+
You can also specify types of models in your calls to defaults, but be careful. This can
|
92
|
+
easily start to become a lot like fixtures:
|
93
|
+
|
94
|
+
module Factory
|
95
|
+
extend ModelFactory
|
96
|
+
|
97
|
+
default User, :joined {
|
98
|
+
:first_name => 'Harry',
|
99
|
+
:last_name => 'Manchester',
|
100
|
+
:joined => true,
|
101
|
+
:set_password => true,
|
102
|
+
}
|
103
|
+
|
104
|
+
default User, :unjoined {
|
105
|
+
:first_name => 'Harry',
|
106
|
+
:last_name => 'Manchester',
|
107
|
+
:joined => false,
|
108
|
+
:set_password => false,
|
109
|
+
}
|
110
|
+
end
|
111
|
+
|
112
|
+
Then in your test:
|
113
|
+
|
114
|
+
def test_something
|
115
|
+
user1 = Factory.create_joined_user(:first_name => 'Bill')
|
116
|
+
user2 = Factory.create_unjoined_user(:first_name => 'Sandy')
|
117
|
+
assert user1.joined?
|
118
|
+
assert !user2.joined?
|
119
|
+
end
|
120
|
+
|
83
121
|
== Installing ModelFactory
|
84
122
|
|
85
123
|
sudo gem install modelfactory
|
data/lib/model_factory.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'rubygems'
|
1
2
|
require 'active_record'
|
2
3
|
|
3
4
|
module ModelFactory
|
@@ -12,9 +13,12 @@ module ModelFactory
|
|
12
13
|
# object as a dependency use the special method default_* instead of
|
13
14
|
# create_* or new_*.
|
14
15
|
#
|
15
|
-
def default(class_type,
|
16
|
+
def default(class_type, *args)
|
17
|
+
defaults = args.pop || {}
|
18
|
+
prefix = args.first
|
16
19
|
class_name = class_type.name.demodulize.underscore
|
17
|
-
|
20
|
+
class_name = "#{prefix}_#{class_name}" if prefix
|
21
|
+
|
18
22
|
(class << self; self; end).module_eval do
|
19
23
|
define_method "create_#{class_name}" do |*args|
|
20
24
|
attributes = args.first || {}
|
@@ -78,10 +82,8 @@ module ModelFactory
|
|
78
82
|
if protected_attrs or accessible_attrs
|
79
83
|
attributes.each do |key, value|
|
80
84
|
# Support symbols and strings.
|
81
|
-
|
82
|
-
|
83
|
-
next if accessible_attrs and accessible_attrs.include?(attr)
|
84
|
-
end
|
85
|
+
next if protected_attrs and not (protected_attrs.include?(key) or protected_attrs.include?(key.to_s))
|
86
|
+
next if accessible_attrs and (accessible_attrs.include?(key) or accessible_attrs.include?(key.to_s))
|
85
87
|
modified = true
|
86
88
|
instance.send("#{key}=", value)
|
87
89
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: modelfactory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Balthrop
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2008-
|
13
|
+
date: 2008-12-10 00:00:00 -08:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -73,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
73
|
requirements: []
|
74
74
|
|
75
75
|
rubyforge_project: modelfactory
|
76
|
-
rubygems_version: 1.
|
76
|
+
rubygems_version: 1.3.1
|
77
77
|
signing_key:
|
78
78
|
specification_version: 2
|
79
79
|
summary: A replacement for fixtures.
|