cranky 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 832320f9e104372957157b3cef6c0d81592a6b66
4
- data.tar.gz: 6560e13c455a033fa20f11041c065bf13b95f7ec
3
+ metadata.gz: c7f6c5a28b743f350225d95efa1a42db23f299aa
4
+ data.tar.gz: 8713bef7ea202b009f97c89625403af0d00fd7d4
5
5
  SHA512:
6
- metadata.gz: 82347e3df728a862aec73692225e6e0344df1503d0ff117ca4f8bff366f8626e7fbc027d7322c32f73b95d11de6a0bd0bbe1c895a1116141e5fbb5c45238e62f
7
- data.tar.gz: a5c223a0a63ad0231fb342c5f20fcd17fbdd438431a05f78fff8fa25fbc00b163f8b1bb6bea24166e36070bab98d7ca3f88812987de034d944aa49f3a251f9e8
6
+ metadata.gz: 6f172f48edbf5686465205c5df9254a3b68b74cc47761d78c8d623511975e0dfc2141bd295c59a1af09ee250eaec743a317cbb14aec3ddb251ed80f58dc36ce7
7
+ data.tar.gz: 1b3f2af3d7bc85e6a25742aecf10cb27ef635cb4c78622d9e4ddb7bfe4361ad8bb9c8adbd5ed02b602aa921895b25759ab282b20b93d536f66f19b8897e5e5d8
data/Gemfile CHANGED
@@ -1,6 +1,7 @@
1
1
  source "http://gemcutter.org"
2
2
 
3
3
  group :development do
4
+ gem "activemodel"
4
5
  gem "rake"
5
6
  gem "gemcutter"
6
7
  end
@@ -1,10 +1,22 @@
1
1
  GEM
2
2
  remote: http://gemcutter.org/
3
3
  specs:
4
+ activemodel (4.2.6)
5
+ activesupport (= 4.2.6)
6
+ builder (~> 3.1)
7
+ activesupport (4.2.6)
8
+ i18n (~> 0.7)
9
+ json (~> 1.7, >= 1.7.7)
10
+ minitest (~> 5.1)
11
+ thread_safe (~> 0.3, >= 0.3.4)
12
+ tzinfo (~> 1.1)
13
+ builder (3.2.2)
4
14
  diff-lcs (1.1.2)
5
15
  docile (1.1.5)
6
16
  gemcutter (0.7.0)
17
+ i18n (0.7.0)
7
18
  json (1.8.3)
19
+ minitest (5.9.0)
8
20
  rake (0.9.2)
9
21
  rspec (2.6.0)
10
22
  rspec-core (~> 2.6.0)
@@ -19,15 +31,19 @@ GEM
19
31
  json (~> 1.8)
20
32
  simplecov-html (~> 0.10.0)
21
33
  simplecov-html (0.10.0)
34
+ thread_safe (0.3.5)
35
+ tzinfo (1.2.2)
36
+ thread_safe (~> 0.1)
22
37
 
23
38
  PLATFORMS
24
39
  ruby
25
40
 
26
41
  DEPENDENCIES
42
+ activemodel
27
43
  gemcutter
28
44
  rake
29
45
  rspec
30
46
  simplecov
31
47
 
32
48
  BUNDLED WITH
33
- 1.12.2
49
+ 1.12.5
data/README.md CHANGED
@@ -207,6 +207,16 @@ def apply_trait_manager_to_user(user)
207
207
  end
208
208
  ~~~
209
209
 
210
+ You can create collections...
211
+
212
+ ~~~ruby
213
+ Factory.create(:users_collection)
214
+
215
+ def users_collection
216
+ 3.time.map { build(:user) }
217
+ end
218
+ ~~~
219
+
210
220
  ## Helpers
211
221
 
212
222
  Of course its nice to get some help...
@@ -6,23 +6,23 @@ module Cranky
6
6
  def initialize
7
7
  # Factory jobs can be nested, i.e. a factory method can itself invoke another factory method to
8
8
  # build a dependent object. In this case jobs the jobs are pushed into a pipeline and executed
9
- # in a last in first out order.
9
+ # in a last in first out order.
10
10
  @pipeline = []
11
11
  @n = 0
12
12
  @errors = []
13
13
  end
14
-
14
+
15
15
  def build(what, overrides={})
16
16
  crank_it(what, overrides)
17
17
  end
18
18
 
19
19
  def create(what, overrides={})
20
20
  item = build(what, overrides)
21
- item.save
21
+ Array(item).each(&:save)
22
22
  item
23
23
  end
24
24
 
25
- # Reset the factory instance, clear all instance variables
25
+ # Reset the factory instance, clear all instance variables
26
26
  def reset
27
27
  self.instance_variables.each do |var|
28
28
  instance_variable_set(var, nil)
@@ -34,17 +34,18 @@ module Cranky
34
34
  build(what, attrs.merge(:_return_attributes => true))
35
35
  end
36
36
 
37
- # Can be left in your tests as an alternative to build and to warn if your factory method
38
- # ever starts producing invalid instances
37
+ # Can be left in your tests as an alternative to build and to warn if your factory method
38
+ # ever starts producing invalid instances
39
39
  def debug(*args)
40
- item = build(*args)
41
- if !item.valid?
42
- if item.errors.respond_to?("messages")
43
- errors = item.errors.messages
40
+ item = build(*args)
41
+ invalid_item = Array(item).find(&:invalid?)
42
+ if invalid_item
43
+ if invalid_item.errors.respond_to?(:messages)
44
+ errors = invalid_item.errors.messages
44
45
  else
45
- errors = item.errors
46
+ errors = invalid_item.errors
46
47
  end
47
- raise "Oops, the #{item.class} created by the Factory has the following errors: #{errors}"
48
+ raise "Oops, the #{invalid_item.class} created by the Factory has the following errors: #{errors}"
48
49
  end
49
50
  item
50
51
  end
@@ -86,10 +87,10 @@ module Cranky
86
87
  item
87
88
  end
88
89
 
89
- # This method actually makes the required object instance, it gets called by the users factory
90
+ # This method actually makes the required object instance, it gets called by the users factory
90
91
  # method, where the name 'define' makes more sense than it does here!
91
92
  def define(defaults={})
92
- current_job.defaults = defaults
93
+ current_job.defaults = defaults
93
94
  current_job.execute
94
95
  end
95
96
 
@@ -100,7 +101,7 @@ module Cranky
100
101
  end
101
102
 
102
103
  def current_job
103
- @pipeline.last
104
+ @pipeline.last
104
105
  end
105
106
 
106
107
  # Returns a hash containing any top-level overrides passed in when the current factory was invoked
@@ -108,7 +109,7 @@ module Cranky
108
109
  current_job.overrides
109
110
  end
110
111
 
111
- # Adds a new job to the pipeline then yields to the caller to execute it
112
+ # Adds a new job to the pipeline then yields to the caller to execute it
112
113
  def new_job(what, overrides)
113
114
  @pipeline << Job.new(what, overrides)
114
115
  yield
@@ -1,3 +1,3 @@
1
1
  module Cranky
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -19,6 +19,11 @@ describe "The Cranky factory" do
19
19
  crank!(:user).saved?.should == true
20
20
  end
21
21
 
22
+ it "does save each item in the collection when generated via create or crank!" do
23
+ Factory.create(:users_collection).select { |u| !u.saved? }.should be_empty
24
+ crank!(:users_collection).select { |u| !u.saved? }.should be_empty
25
+ end
26
+
22
27
  it "allows all attributes to be overriden in the call" do
23
28
  u = Factory.build(:user, :name => "Indy", :email => "indy@home.com", :role => :dog, :unique => :yep)
24
29
  u.name.should == "Indy"
@@ -74,19 +79,15 @@ describe "The Cranky factory" do
74
79
  describe "debugger" do
75
80
 
76
81
  it "raises an error if the factory produces an invalid object when enabled (rails only)" do
77
- error = false
78
- begin
79
- Factory.debug(:user, :valid => false)
80
- rescue
81
- error = true
82
- end
83
- error.should == true
82
+ expect { Factory.debug(:user) }.to raise_error(
83
+ 'Oops, the User created by the Factory has the following errors: {:required_attr=>["can\'t be blank"]}'
84
+ )
84
85
  end
85
86
 
86
87
  it "debug works like build and create when there are no errors" do
87
- Factory.debug(:user).class.should == User
88
- Factory.debug(:user).saved?.should == false
89
- Factory.debug!(:user).saved?.should == true
88
+ Factory.debug(:user, required_attr: true).class.should == User
89
+ Factory.debug(:user, required_attr: true).saved?.should == false
90
+ Factory.debug!(:user, required_attr: true).saved?.should == true
90
91
  end
91
92
 
92
93
  end
@@ -146,7 +147,7 @@ describe "The Cranky factory" do
146
147
  end
147
148
 
148
149
  it "returns nothing extra in the attributes" do
149
- crank(:user_attrs).size.should == 6
150
+ crank(:user_attrs).size.should == 5
150
151
  end
151
152
 
152
153
  specify "attributes for works with factory methods using inherit" do
@@ -2,13 +2,18 @@ require 'simplecov'
2
2
  SimpleCov.start
3
3
 
4
4
  require 'cranky'
5
+ require 'active_model'
5
6
 
6
7
  RSpec.configure do |config|
7
8
  config.expect_with(:rspec) { |c| c.syntax = :should }
8
9
  end
9
10
 
10
11
  class TestClass
11
- attr_accessor :valid
12
+ include ActiveModel::Validations
13
+
14
+ attr_accessor :required_attr
15
+
16
+ validates_presence_of :required_attr
12
17
 
13
18
  def save
14
19
  @saved = true
@@ -18,14 +23,6 @@ class TestClass
18
23
  !!@saved
19
24
  end
20
25
 
21
- def valid?
22
- @valid
23
- end
24
-
25
- def errors
26
- "some validation errors"
27
- end
28
-
29
26
  def attributes
30
27
  self.instance_variables
31
28
  end
@@ -59,7 +56,6 @@ class Cranky::Factory
59
56
  u.unique = "value#{n}"
60
57
  u.email = "fred@home.com"
61
58
  u.address = Factory.build(:address)
62
- u.valid = true
63
59
  u
64
60
  end
65
61
 
@@ -69,8 +65,7 @@ class Cranky::Factory
69
65
  :role => :user,
70
66
  :unique => "value#{n}",
71
67
  :email => "fred@home.com",
72
- :address => Factory.create(:address),
73
- :valid => true
68
+ :address => Factory.create(:address)
74
69
  u.argument_received = true if options[:argument_supplied]
75
70
  u
76
71
  end
@@ -86,8 +81,7 @@ class Cranky::Factory
86
81
 
87
82
  def address
88
83
  define :address => "25 Wisteria Lane",
89
- :city => "New York",
90
- :valid => true
84
+ :city => "New York"
91
85
  end
92
86
 
93
87
  def user_hash
@@ -96,6 +90,10 @@ class Cranky::Factory
96
90
  :role => :user
97
91
  end
98
92
 
93
+ def users_collection
94
+ 3.times.map { build(:user) }
95
+ end
96
+
99
97
  def apply_trait_manager_to_user_manually(user)
100
98
  user.role = :manager
101
99
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cranky
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen McGinty
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-30 00:00:00.000000000 Z
11
+ date: 2016-08-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A very light yet powerful test factory framework with an extremely clean
14
14
  syntax that makes it very easy to define your factories.
@@ -49,7 +49,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
49
  version: 1.3.4
50
50
  requirements: []
51
51
  rubyforge_project:
52
- rubygems_version: 2.4.5
52
+ rubygems_version: 2.6.4
53
53
  signing_key:
54
54
  specification_version: 4
55
55
  summary: A very light yet powerful test factory framework.