cranky 0.2.0 → 0.3.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/CHANGELOG.md +26 -0
- data/Gemfile +3 -1
- data/Gemfile.lock +29 -4
- data/README.rdoc +13 -8
- data/lib/cranky/factory.rb +11 -2
- data/lib/cranky/job.rb +30 -9
- data/lib/cranky/version.rb +1 -1
- data/spec/cranky_spec.rb +44 -20
- data/spec/spec_helper.rb +6 -1
- metadata +8 -9
data/CHANGELOG.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# Overview of Changes
|
2
|
+
|
3
|
+
## 0.3.0
|
4
|
+
|
5
|
+
* Updated README to present the crank syntax as the default
|
6
|
+
|
7
|
+
* Appending the first argument with _attrs will return a hash of attributes rather than
|
8
|
+
the object, i.e. crank(:user) returns a User instance, and crank(:user_attrs) returns
|
9
|
+
a hash of valid attributes
|
10
|
+
|
11
|
+
* Factory.attributes_for now returns a hash of attributes rather than an array, this
|
12
|
+
method is no longer dependent on rails
|
13
|
+
|
14
|
+
* \#3 Factory methods can generate attribute hashes by default by setting :class => Hash in
|
15
|
+
the define declaration (leemhenson)
|
16
|
+
|
17
|
+
* Factory.debug was broken on ActiveModel based classes, now works with them
|
18
|
+
|
19
|
+
* Attributes can be skipped by setting the value to :skip. e.g. crank(:user, :name => :skip) will
|
20
|
+
generate a User instance without calling or assigning anything to the name attribute
|
21
|
+
|
22
|
+
* Updated dev environment for latest rspec compatibility (2.6.0)
|
23
|
+
|
24
|
+
## 0.2.0
|
25
|
+
|
26
|
+
Changes between version prior to this were not tracked here
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,14 +1,39 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://gemcutter.org/
|
3
3
|
specs:
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
archive-tar-minitar (0.5.2)
|
5
|
+
columnize (0.3.4)
|
6
|
+
diff-lcs (1.1.2)
|
7
|
+
gemcutter (0.7.0)
|
8
|
+
linecache19 (0.5.12)
|
9
|
+
ruby_core_source (>= 0.1.4)
|
10
|
+
rake (0.9.2)
|
11
|
+
rcov (0.9.9)
|
12
|
+
rspec (2.6.0)
|
13
|
+
rspec-core (~> 2.6.0)
|
14
|
+
rspec-expectations (~> 2.6.0)
|
15
|
+
rspec-mocks (~> 2.6.0)
|
16
|
+
rspec-core (2.6.4)
|
17
|
+
rspec-expectations (2.6.0)
|
18
|
+
diff-lcs (~> 1.1.2)
|
19
|
+
rspec-mocks (2.6.0)
|
20
|
+
ruby-debug-base19 (0.11.25)
|
21
|
+
columnize (>= 0.3.1)
|
22
|
+
linecache19 (>= 0.5.11)
|
23
|
+
ruby_core_source (>= 0.1.4)
|
24
|
+
ruby-debug19 (0.11.6)
|
25
|
+
columnize (>= 0.3.1)
|
26
|
+
linecache19 (>= 0.5.11)
|
27
|
+
ruby-debug-base19 (>= 0.11.19)
|
28
|
+
ruby_core_source (0.1.5)
|
29
|
+
archive-tar-minitar (>= 0.5.2)
|
7
30
|
|
8
31
|
PLATFORMS
|
9
32
|
ruby
|
10
33
|
|
11
34
|
DEPENDENCIES
|
12
35
|
gemcutter
|
36
|
+
rake
|
13
37
|
rcov
|
14
|
-
rspec
|
38
|
+
rspec
|
39
|
+
ruby-debug19
|
data/README.rdoc
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
= Cranky
|
2
2
|
|
3
|
-
Cranky is a fixtures replacement inspired by the excellent Factory Girl
|
3
|
+
Cranky is a fixtures replacement inspired by the excellent Factory Girl but with easier syntax and no external
|
4
|
+
dependencies, it will work straight out of the box with rails 10.
|
4
5
|
|
5
6
|
In short use this if you want to quickly and naturally create factory methods that you feel 100% in control of.
|
6
7
|
|
@@ -10,10 +11,7 @@ First install the gem...
|
|
10
11
|
|
11
12
|
gem install cranky
|
12
13
|
|
13
|
-
Or
|
14
|
-
|
15
|
-
# environment.rb
|
16
|
-
config.gem "cranky"
|
14
|
+
Or with bundler...
|
17
15
|
|
18
16
|
# Gemfile
|
19
17
|
gem "cranky"
|
@@ -29,7 +27,14 @@ You can create as many different factory files as you want, just require them in
|
|
29
27
|
|
30
28
|
== In a Nutshell
|
31
29
|
|
32
|
-
|
30
|
+
The API to use in your tests is:
|
31
|
+
|
32
|
+
crank(:user) # Build a user instance without saving
|
33
|
+
crank!(:user) # Build and save a user instance
|
34
|
+
crank(:user, :name => "Ian") # Override a default attribute value
|
35
|
+
crank(:user_attrs) # Return a set of valid attributes rather than the object
|
36
|
+
|
37
|
+
Alternatively the Factory Girl syntax also works and therefore Cranky can drop into tests already written for that framework...
|
33
38
|
|
34
39
|
Factory.build(:user) # Build a user instance without saving
|
35
40
|
Factory.create(:user) # Build and save a user instance
|
@@ -50,7 +55,7 @@ Cranky has a nice debug option (rails only) to warn you when your factory is bro
|
|
50
55
|
Cranky allows you to build factories via std Ruby methods, like this...
|
51
56
|
|
52
57
|
# factories/my_factories.rb
|
53
|
-
class Cranky::Factory # Your factory must
|
58
|
+
class Cranky::Factory # Your factory must reopen Cranky::Factory
|
54
59
|
|
55
60
|
# Simple factory method to create a user instance, you would call this via Factory.build(:user)
|
56
61
|
def user
|
@@ -89,7 +94,7 @@ This is where Cranky really shines, if you can create Ruby methods you can prett
|
|
89
94
|
|
90
95
|
The only rules are:
|
91
96
|
|
92
|
-
1. Your factory must
|
97
|
+
1. Your factory must reopen the Cranky::Factory class
|
93
98
|
2. Your factory method must return the object you wanted to create
|
94
99
|
3. You can access the overrides passed in via options[:key] (not really a rule!)
|
95
100
|
|
data/lib/cranky/factory.rb
CHANGED
@@ -31,7 +31,7 @@ module Cranky
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def attributes_for(what, attrs={})
|
34
|
-
build(what, attrs)
|
34
|
+
build(what, attrs.merge(:_return_attributes => true))
|
35
35
|
end
|
36
36
|
|
37
37
|
# Can be left in your tests as an alternative to build and to warn if your factory method
|
@@ -39,7 +39,12 @@ module Cranky
|
|
39
39
|
def debug(*args)
|
40
40
|
item = build(*args)
|
41
41
|
if !item.valid?
|
42
|
-
|
42
|
+
if item.errors.respond_to?("messages")
|
43
|
+
errors = item.errors.messages
|
44
|
+
else
|
45
|
+
errors = item.errors
|
46
|
+
end
|
47
|
+
raise "Oops, the #{item.class} created by the Factory has the following errors: #{errors}"
|
43
48
|
end
|
44
49
|
item
|
45
50
|
end
|
@@ -63,6 +68,10 @@ module Cranky
|
|
63
68
|
|
64
69
|
# Execute the requested factory method, crank out the target object!
|
65
70
|
def crank_it(what, overrides)
|
71
|
+
if what.to_s =~ /(.*)_attrs$/
|
72
|
+
what = $1
|
73
|
+
overrides = overrides.merge(:_return_attributes => true)
|
74
|
+
end
|
66
75
|
item = "TBD"
|
67
76
|
new_job(what, overrides) do
|
68
77
|
item = self.send(what) # Invoke the factory method
|
data/lib/cranky/job.rb
CHANGED
@@ -8,6 +8,7 @@ module Cranky
|
|
8
8
|
@defaults = {}
|
9
9
|
@target = target
|
10
10
|
@overrides = overrides
|
11
|
+
@return_attributes = overrides.delete(:_return_attributes)
|
11
12
|
end
|
12
13
|
|
13
14
|
def attributes
|
@@ -19,23 +20,43 @@ module Cranky
|
|
19
20
|
@defaults = defs
|
20
21
|
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
item
|
23
|
+
# Returns the created item
|
24
|
+
def item
|
25
|
+
return @item if @item
|
26
|
+
if @return_attributes
|
27
|
+
@item = Hash.new
|
28
|
+
else
|
29
|
+
@item = get_constant(attributes[:class] ? attributes.delete(:class) : @target).new
|
27
30
|
end
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
+
end
|
32
|
+
|
33
|
+
# Assign the value to the given attribute of the item
|
34
|
+
def assign(attribute, value)
|
35
|
+
unless value == :skip || attribute == :class
|
36
|
+
if item.respond_to?("#{attribute}=")
|
37
|
+
item.send("#{attribute}=", value)
|
38
|
+
elsif item.is_a?(Hash)
|
39
|
+
item[attribute] = value
|
40
|
+
end
|
31
41
|
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def execute
|
45
|
+
values = attributes.reject { |attribute, value| value.respond_to?("call") }
|
46
|
+
blocks = attributes.select { |attribute, value| value.respond_to?("call") }
|
47
|
+
|
48
|
+
values.each { |attribute, value| assign(attribute, value) }
|
49
|
+
blocks.each { |attribute, value| assign(attribute, value.call(*(value.arity > 0 ? [item] : []))) }
|
50
|
+
|
32
51
|
item
|
33
52
|
end
|
34
53
|
|
35
54
|
private
|
36
55
|
|
37
|
-
# Nicked from here: http://gist.github.com/301173
|
56
|
+
# Nicked from here: http://gist.github.com/301173
|
38
57
|
def get_constant(name_sym)
|
58
|
+
return name_sym if name_sym.is_a? Class
|
59
|
+
|
39
60
|
name = name_sym.to_s.split('_').collect {|s| s.capitalize }.join('')
|
40
61
|
Object.const_defined?(name) ? Object.const_get(name) : Object.const_missing(name)
|
41
62
|
end
|
data/lib/cranky/version.rb
CHANGED
data/spec/cranky_spec.rb
CHANGED
@@ -5,19 +5,21 @@ describe "The Cranky factory" do
|
|
5
5
|
before(:each) do
|
6
6
|
end
|
7
7
|
|
8
|
-
it "
|
8
|
+
it "is alive" do
|
9
9
|
Factory.build(:user).class.should == User
|
10
10
|
end
|
11
11
|
|
12
|
-
it "
|
12
|
+
it "does not save the item when generated via build or crank" do
|
13
13
|
Factory.build(:user).saved?.should == false
|
14
|
+
crank(:user).saved?.should == false
|
14
15
|
end
|
15
16
|
|
16
|
-
it "
|
17
|
+
it "does save the item when generated via create or crank!" do
|
17
18
|
Factory.create(:user).saved?.should == true
|
19
|
+
crank!(:user).saved?.should == true
|
18
20
|
end
|
19
21
|
|
20
|
-
it "
|
22
|
+
it "allows all attributes to be overriden in the call" do
|
21
23
|
u = Factory.build(:user, :name => "Indy", :email => "indy@home.com", :role => :dog, :unique => :yep)
|
22
24
|
u.name.should == "Indy"
|
23
25
|
u.email.should == "indy@home.com"
|
@@ -25,19 +27,18 @@ describe "The Cranky factory" do
|
|
25
27
|
u.unique.should == :yep
|
26
28
|
end
|
27
29
|
|
28
|
-
it "
|
29
|
-
|
30
|
-
attrs.class.should == Array
|
30
|
+
it "returns valid attributes rather than the model" do
|
31
|
+
Factory.attributes_for(:user).class.should == Hash
|
31
32
|
end
|
32
33
|
|
33
|
-
it "
|
34
|
+
it "clears all instance variables when reset" do
|
34
35
|
Factory.some_instance_variable = true
|
35
36
|
Factory.some_instance_variable.should == true
|
36
37
|
Factory.reset
|
37
38
|
Factory.some_instance_variable.should == nil
|
38
39
|
end
|
39
40
|
|
40
|
-
it "
|
41
|
+
it "can create items using the define helper or manually" do
|
41
42
|
m = Factory.build(:user_manually)
|
42
43
|
d = Factory.build(:user_by_define)
|
43
44
|
m.name.should == d.name
|
@@ -45,7 +46,7 @@ describe "The Cranky factory" do
|
|
45
46
|
m.role.should == d.role
|
46
47
|
end
|
47
48
|
|
48
|
-
it "
|
49
|
+
it "can build by inheriting and overriding from other methods" do
|
49
50
|
a = Factory.build(:admin_manually)
|
50
51
|
a.name.should == "Fred"
|
51
52
|
a.role.should == :admin
|
@@ -54,14 +55,14 @@ describe "The Cranky factory" do
|
|
54
55
|
b.role.should == :admin
|
55
56
|
end
|
56
57
|
|
57
|
-
it "
|
58
|
+
it "gives top priority to attributes defined at the top level, even when inheriting" do
|
58
59
|
a = Factory.build(:admin_manually, :role => :something_else)
|
59
60
|
a.role.should == :something_else
|
60
61
|
b = Factory.build(:admin_by_define, :role => :something_else)
|
61
62
|
b.role.should == :something_else
|
62
63
|
end
|
63
64
|
|
64
|
-
it "
|
65
|
+
it "creates unique values using the n method" do
|
65
66
|
a = Factory.build(:user)
|
66
67
|
b = Factory.build(:user)
|
67
68
|
c = Factory.build(:user)
|
@@ -72,7 +73,7 @@ describe "The Cranky factory" do
|
|
72
73
|
|
73
74
|
describe "debugger" do
|
74
75
|
|
75
|
-
it "
|
76
|
+
it "raises an error if the factory produces an invalid object when enabled (rails only)" do
|
76
77
|
error = false
|
77
78
|
begin
|
78
79
|
Factory.debug(:user, :valid => false)
|
@@ -82,7 +83,7 @@ describe "The Cranky factory" do
|
|
82
83
|
error.should == true
|
83
84
|
end
|
84
85
|
|
85
|
-
it "
|
86
|
+
it "debug works like build and create when there are no errors" do
|
86
87
|
Factory.debug(:user).class.should == User
|
87
88
|
Factory.debug(:user).saved?.should == false
|
88
89
|
Factory.debug!(:user).saved?.should == true
|
@@ -90,16 +91,16 @@ describe "The Cranky factory" do
|
|
90
91
|
|
91
92
|
end
|
92
93
|
|
93
|
-
it "
|
94
|
+
it "allows arguments to be passed in the overrides hash" do
|
94
95
|
Factory.build(:user).argument_received.should == nil
|
95
96
|
Factory.build(:user, :argument_supplied => true).argument_received.should == true
|
96
97
|
end
|
97
98
|
|
98
|
-
it "
|
99
|
+
it "allows blocks to be passed into the define method" do
|
99
100
|
Factory.build(:user, :name => "jenny", :email => lambda{ |u| "#{u.name}@home.com" }).email.should == "jenny@home.com"
|
100
|
-
Factory.build(:user, :name => lambda{"jimmy" + " cranky"}).name.should == "jimmy cranky"
|
101
|
+
Factory.build(:user, :name => lambda { |u| "jimmy" + " cranky" }).name.should == "jimmy cranky"
|
101
102
|
Factory.build(:user, :name => "jenny", :email => Proc.new{ |u| "#{u.name}@home.com" }).email.should == "jenny@home.com"
|
102
|
-
Factory.build(:user, :name => Proc.new{"jimmy" + " cranky"}).name.should == "jimmy cranky"
|
103
|
+
Factory.build(:user, :name => Proc.new{ |u| "jimmy" + " cranky" }).name.should == "jimmy cranky"
|
103
104
|
end
|
104
105
|
|
105
106
|
it "allows factories to call other factories" do
|
@@ -111,10 +112,33 @@ describe "The Cranky factory" do
|
|
111
112
|
Factory.create(:user_by_define).address.saved?.should == true
|
112
113
|
end
|
113
114
|
|
114
|
-
it "
|
115
|
+
it "has its own syntax" do
|
115
116
|
crank(:user).saved?.should == false
|
116
117
|
crank!(:address).saved?.should == true
|
117
118
|
end
|
118
|
-
|
119
|
+
|
120
|
+
it "is capable of creating user hashes" do
|
121
|
+
Factory.build(:user_hash)[:name].should == "Fred"
|
122
|
+
Factory.build(:user_hash)[:role].should == :user
|
123
|
+
Factory.build(:user_hash)[:class].should be_nil
|
124
|
+
end
|
125
|
+
|
126
|
+
it "is capable of overriding user hashes" do
|
127
|
+
Factory.build(:user_hash, :role => :admin)[:role].should == :admin
|
128
|
+
end
|
129
|
+
|
130
|
+
specify "attributes are not assigned when they have the value :skip" do
|
131
|
+
crank(:user, :name => :skip).name.should_not be
|
132
|
+
end
|
133
|
+
|
134
|
+
it "returns an hash of attributes when the 1st argument is appended with _attrs" do
|
135
|
+
crank(:user_attrs).class.should == Hash
|
136
|
+
crank(:user_attrs, :name => "Yo")[:name].should == "Yo"
|
137
|
+
end
|
138
|
+
|
139
|
+
it "returns nothing extra in the attributes" do
|
140
|
+
crank(:user_attrs).size.should == 6
|
141
|
+
end
|
142
|
+
|
119
143
|
end
|
120
144
|
|
data/spec/spec_helper.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'rubygems'
|
2
|
-
require '
|
2
|
+
require 'rspec'
|
3
3
|
require 'cranky'
|
4
4
|
|
5
5
|
|
@@ -86,6 +86,11 @@ class Cranky::Factory
|
|
86
86
|
:valid => true
|
87
87
|
end
|
88
88
|
|
89
|
+
def user_hash
|
90
|
+
define :class => Hash,
|
91
|
+
:name => "Fred",
|
92
|
+
:role => :user
|
93
|
+
end
|
89
94
|
end
|
90
95
|
|
91
96
|
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cranky
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 23
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
|
-
-
|
7
|
+
- 3
|
9
8
|
- 0
|
10
|
-
version: 0.
|
9
|
+
version: 0.3.0
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Stephen McGinty
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date:
|
17
|
+
date: 2011-10-16 00:00:00 +01:00
|
19
18
|
default_executable:
|
20
19
|
dependencies: []
|
21
20
|
|
@@ -28,16 +27,17 @@ extensions: []
|
|
28
27
|
extra_rdoc_files: []
|
29
28
|
|
30
29
|
files:
|
31
|
-
- lib/cranky/job.rb
|
32
30
|
- lib/cranky/factory.rb
|
33
31
|
- lib/cranky/version.rb
|
32
|
+
- lib/cranky/job.rb
|
34
33
|
- lib/cranky.rb
|
35
34
|
- spec/spec_helper.rb
|
36
35
|
- spec/cranky_spec.rb
|
36
|
+
- CHANGELOG.md
|
37
|
+
- Gemfile
|
38
|
+
- Gemfile.lock
|
37
39
|
- README.rdoc
|
38
40
|
- LICENSE
|
39
|
-
- Gemfile.lock
|
40
|
-
- Gemfile
|
41
41
|
- init.rb
|
42
42
|
has_rdoc: true
|
43
43
|
homepage: http://github.com/ginty/cranky
|
@@ -53,7 +53,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
53
53
|
requirements:
|
54
54
|
- - ">="
|
55
55
|
- !ruby/object:Gem::Version
|
56
|
-
hash:
|
56
|
+
hash: -860389489
|
57
57
|
segments:
|
58
58
|
- 0
|
59
59
|
version: "0"
|
@@ -62,7 +62,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
62
|
requirements:
|
63
63
|
- - ">="
|
64
64
|
- !ruby/object:Gem::Version
|
65
|
-
hash: 19
|
66
65
|
segments:
|
67
66
|
- 1
|
68
67
|
- 3
|