unidom-common-rspec 0.6 → 0.6.1

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: 9bc9d62357cf28e32abbdbcdac81fe842e6ba045
4
- data.tar.gz: 65338e314cbb0633f8a586856472bcc27f7bbf07
3
+ metadata.gz: 2da7a6fa1baf6889f7e39b8f4e24fece96ba6596
4
+ data.tar.gz: e93a8669818a91ed003ebfc1e5d372719e35bd1f
5
5
  SHA512:
6
- metadata.gz: 439f897d6c939e3943ffc90e68808b8d2f0f571b50caa000590019e7ab4d8d727f629cec941e43567f424c0faf4561d5874c588313d6c9a96578f6434224c738
7
- data.tar.gz: 0c54ac2b809fe7d97bab55a7e67fc004afa8b1687548c46dc46741da00923d4572fecde4de55437f0e7e9d4b6228005d9eaf13ba5d3ee96f6b5cf14171738141
6
+ metadata.gz: 7795d2019dd3b40cbba76146cd0b3f398730005ea23a8c0890f1f0931bd354a2eb41bc9415314e0ded693878b6775bfd965a27fe5fa3fa251a9fede13cefd29f
7
+ data.tar.gz: 013d1670898e0173062130aa12da57719cbc88c48af527f9e959a76feede563b49a025cc82c0490bb2047cd124b92864648ba7093f138a51f9d873394b79457d
data/CHANGELOG.md CHANGED
@@ -19,3 +19,7 @@
19
19
 
20
20
  ## v0.6
21
21
  1. Has One shared examples
22
+
23
+ ## v0.6.1
24
+ 1. Improve the Belongs To shared examples for the #build_association method, the #create_association method, & the #create_association! method
25
+ 2. Improve the Has One shared examples for the #build_association method, the #create_association method, & the #create_association! method
data/README.md CHANGED
@@ -35,7 +35,7 @@ Or install it yourself as:
35
35
 
36
36
  ## Usage
37
37
 
38
- Assume we have the Person model & the Pet model as the following:
38
+ Assume we have the Person model, the Pet model, & the Identity Card model as the following:
39
39
  ```ruby
40
40
  # person.rb
41
41
  class Person < ApplicationRecord
@@ -130,79 +130,79 @@ describe Person, type: :model do
130
130
  end
131
131
  ```
132
132
 
133
- ### Has Many shared examples Has Many 共享用例
133
+ ### Belongs To shared examples Belongs To 共享用例
134
134
 
135
- The ``person_spec.rb`` looks like the following:
135
+ The ``pet_spec.rb`` looks like the following:
136
136
  ```ruby
137
137
  require 'rails_helper'
138
138
 
139
- describe Person, type: :model do
139
+ describe Pet, type: :model do
140
140
 
141
141
  context do
142
142
 
143
+ cat_attributes = { name: 'Pearl', species: 'Persian' }
143
144
  tim_attributes = { name: 'Tim' }
144
- cat_attributes = { name: 'Pearl', species: 'Persian' }
145
- dog_attribtues = { name: 'Flower', species: 'Chihuahua' }
146
145
 
147
- it_behaves_like 'has_many', tim_attributes, :pets, Pet, [ cat_attributes, dog_attribtues ]
146
+ it_behaves_like 'belongs_to', cat_attributes, :person, Person, tim_attributes
148
147
 
149
148
  end
150
149
 
151
150
  end
152
151
  ```
153
152
 
154
- ### Has One shared examples Has Many 共享用例
155
-
156
- The ``person_spec.rb`` looks like the following:
153
+ The ``identity_card_spec.rb`` looks like the following:
157
154
  ```ruby
158
155
  require 'rails_helper'
159
156
 
160
- describe Person, type: :model do
157
+ describe IdentityCard, type: :model do
161
158
 
162
159
  context do
163
160
 
164
161
  tim_attributes = { name: 'Tim' }
165
162
  tim_identity_card_attributes = { name: 'Tim', gender_code: '1', birth_date: '1980-07-01' }
166
163
 
167
- it_behaves_like 'has_one', tim_attributes, :identity_card, IdentityCard, tim_identity_card_attributes
164
+ it_behaves_like 'belongs_to', tim_identity_card_attributes, :person, Person, tim_attributes
168
165
 
169
166
  end
170
167
 
171
168
  end
172
169
  ```
173
170
 
174
- ### Belongs To shared examples Belongs To 共享用例
171
+ ### Has Many shared examples Has Many 共享用例
175
172
 
176
- The ``pet_spec.rb`` looks like the following:
173
+ The ``person_spec.rb`` looks like the following:
177
174
  ```ruby
178
175
  require 'rails_helper'
179
176
 
180
- describe Pet, type: :model do
177
+ describe Person, type: :model do
181
178
 
182
179
  context do
183
180
 
184
- cat_attributes = { name: 'Pearl', species: 'Persian' }
185
181
  tim_attributes = { name: 'Tim' }
182
+ cat_attributes = { name: 'Pearl', species: 'Persian' }
183
+ dog_attribtues = { name: 'Flower', species: 'Chihuahua' }
186
184
 
187
- it_behaves_like 'belongs_to', cat_attributes, :person, Person, tim_attributes
185
+ it_behaves_like 'has_many', tim_attributes, :pets, Pet, [ cat_attributes, dog_attribtues ]
188
186
 
189
187
  end
190
188
 
191
189
  end
192
190
  ```
193
191
 
194
- The ``identity_card_spec.rb`` looks like the following:
192
+ ### Has One shared examples Has Many 共享用例
193
+
194
+ The ``person_spec.rb`` looks like the following:
195
195
  ```ruby
196
196
  require 'rails_helper'
197
197
 
198
- describe IdentityCard, type: :model do
198
+ describe Person, type: :model do
199
199
 
200
200
  context do
201
201
 
202
202
  tim_attributes = { name: 'Tim' }
203
203
  tim_identity_card_attributes = { name: 'Tim', gender_code: '1', birth_date: '1980-07-01' }
204
204
 
205
- it_behaves_like 'belongs_to', tim_identity_card_attributes, :person, Person, tim_attributes
205
+ it_behaves_like 'has_one', tim_attributes, :identity_card, IdentityCard, tim_identity_card_attributes
206
206
 
207
207
  end
208
208
 
@@ -228,6 +228,8 @@ describe Person, type: :model do
228
228
  end
229
229
  ```
230
230
 
231
+
232
+
231
233
  ## Development
232
234
 
233
235
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/ROADMAP.md CHANGED
@@ -19,3 +19,10 @@
19
19
 
20
20
  ## v0.6
21
21
  1. Has One shared examples
22
+
23
+ ## v0.6.1
24
+ 1. Improve the Belongs To shared examples for the #build_association method, the #create_association method, & the #create_association! method
25
+ 2. Improve the Has One shared examples for the #build_association method, the #create_association method, & the #create_association! method
26
+
27
+ ## v0.6.2
28
+ 1. Improve the Has Many shared examples for the #build_association method, the #create_association method, & the #create_association! method
@@ -1,3 +1,12 @@
1
+ ##
2
+ # The ``belongs_to :associated_model`` macro uses the ActiveRecord::Associations::BelongsToAssociation.
3
+ # This association generates the following 5 methods:
4
+ # #associated_model, #associated_model=, #build_associated_model, #create_associated_model, and #create_associated_model!.
5
+ #
6
+ # The ``belongs_to :associated_model, polymorphic: true`` macro uses the ActiveRecord::Associations::BelongsToPolymorphicAssociation.
7
+ # This association generates the 2 methods: #associated_model and #associated_model=.
8
+ # It does not generate the 3 methods: #build_associated_model, #create_associated_model, and #create_associated_model!
9
+
1
10
  shared_examples 'belongs_to' do |model_attributes, association_name, association_class, association_attributes|
2
11
 
3
12
  describe "##{association_name}: #{association_class.name}" do
@@ -5,32 +14,49 @@ shared_examples 'belongs_to' do |model_attributes, association_name, association
5
14
  model_instance = described_class.new model_attributes
6
15
  association_instance = association_class.new association_attributes
7
16
  association = model_instance.association association_name.to_sym
8
- association_writable = association.class==ActiveRecord::Associations::BelongsToAssociation
17
+ association_writable = [ ActiveRecord::Associations::BelongsToAssociation ].include? association.class
9
18
 
10
- methods = [ association_name, "#{association_name}=" ]
11
- methods = methods+[ "build_#{association_name}", "create_#{association_name}", "create_#{association_name}!" ] if association_writable
19
+ gained_methods = [ association_name.to_sym ]
20
+ gained_methods << [ :"#{association_name}=", :"build_#{association_name}", :"create_#{association_name}", :"create_#{association_name}!" ] if association_writable
21
+ gained_methods.flatten!
12
22
 
13
- methods.each do |method|
14
- describe "##{method}" do
23
+ subject do described_class.new model_attributes end
15
24
 
16
- it 'is responded to' do expect(model_instance).to respond_to(association_name.to_sym) end
25
+ gained_methods.each do |method|
26
+ it do is_expected.to respond_to(method.to_sym) end
27
+ end
17
28
 
18
- case method
29
+ describe "#{association_name}=" do
30
+ before :each do
31
+ subject.send "#{association_name}=", nil
32
+ subject.send "#{association_name}=", association_instance
33
+ end
34
+ it 'should return the assigned association' do
35
+ expect(subject.send association_name.to_sym).to eq(association_instance)
36
+ end
37
+ end
19
38
 
20
- when "#{association_name}="
21
- it 'assigns association successfully' do
22
- model_instance.send "#{association_name}=".to_sym, association_instance
23
- expect(model_instance.send association_name.to_sym).to eq(association_instance)
24
- end
39
+ if association_writable
25
40
 
26
- when association_name
27
- it 'saves successfully' do
28
- model_instance.send "#{association_name}=".to_sym, association_instance
29
- expect(model_instance.save).to be_truthy
30
- end
31
- end
41
+ context "#build_#{association_name}" do
42
+ let :association_instance_1 do subject.send "build_#{association_name}" end
43
+ let :association_instance_2 do subject.send "build_#{association_name}" end
44
+ it "should be a #{association_class.name}" do expect(association_instance_1).to be_a(association_class) end
45
+ it 'should not be a new record' do expect(association_instance_1).to be_new_record end
46
+ it "should be a #{association_class.name}" do expect(association_instance_2).to be_a(association_class) end
47
+ it 'should not be a new record' do expect(association_instance_2).to be_new_record end
48
+ it 'should not be identical' do expect(association_instance_1).to_not be(association_instance_2) end
49
+ end
32
50
 
51
+ [ "create_#{association_name}", "create_#{association_name}!" ].each do |method_name|
52
+ context "##{method_name}" do
53
+ subject do described_class.create! model_attributes end
54
+ let :created_association_instance do subject.send method_name, association_attributes end
55
+ it "should be a #{association_class.name}" do expect(created_association_instance).to be_an_instance_of(association_class) end
56
+ it 'should not be a new record' do expect(created_association_instance).to_not be_new_record end
57
+ end
33
58
  end
59
+
34
60
  end
35
61
 
36
62
  end
@@ -10,116 +10,62 @@ shared_examples 'has_one' do |model_attributes, association_name, association_cl
10
10
  gained_methods << [ :"#{association_name}=", :"build_#{association_name}", :"create_#{association_name}", :"create_#{association_name}!" ] if association_writable
11
11
  gained_methods.flatten!
12
12
 
13
- #subject :model_instance do described_class.new model_attributes end
14
- #let :association_instance do association_class.new association_attributes end
15
- #let :reflection do model_instance.association(association_name.to_sym).reflection end
16
- #let :association_writable do reflection.class==ActiveRecord::Reflection::HasOneReflection end
13
+ subject do described_class.new model_attributes end
17
14
 
18
- before :each do
19
- subject do
20
- described_class.new model_attributes
21
- end
15
+ gained_methods.each do |method|
16
+ it do is_expected.to respond_to(method.to_sym) end
22
17
  end
23
18
 
24
- after :each do
25
- subject do
26
- nil
19
+ describe "#{association_name}=" do
20
+ before :each do
21
+ subject.send "#{association_name}=", nil
22
+ subject.send "#{association_name}=", association_instance
27
23
  end
28
- end
29
-
30
- #describe do
31
- gained_methods.each do |method|
32
- it do is_expected.to respond_to(method.to_sym) end
24
+ it 'should return the assigned association' do
25
+ expect(subject.send association_name.to_sym).to eq(association_instance)
33
26
  end
34
- #end
35
-
36
- it 'assigns association successfully' do
37
- model_instance.send "#{association_name}=", association_instance
38
- expect(model_instance.send association_name.to_sym).to eq(association_instance)
39
27
  end
40
- =begin
41
- if association_writable
42
- context do
43
- it 'builds an association instance' do
44
- item_1 = model_instance.send "build_#{association_name}"
45
- expect(item_1).to be_an_instance_of(association_class)
46
- expect(item_1).to be_new_record
47
- item_2 = model_instance.send "build_#{association_name}"
48
- expect(item_2).to be_an_instance_of(association_class)
49
- expect(item_2).to be_new_record
50
- expect(item_1).to_not be(item_2)
51
- end
52
- end
53
- context do
54
- before :each do
55
- puts '---- before each...'
56
- subject do described_class.new model_attributes end
57
- puts "----!!!! subject = #{subject.inspect}"
58
- puts "subject.clear_text = #{subject.clear_text}"
59
- puts "model_attributes = #{model_attributes.inspect}"
60
- end
61
- it 'creates an association instance' do
62
- subject do described_class.new model_attributes end
63
- subject.valid?
64
- puts "---- errors = #{subject.errors.inspect}"
65
- puts "---- **** subject = #{subject.inspect}"
66
28
 
67
- expect(subject.save).to be_truthy
29
+ if association_writable
68
30
 
69
- created_association_instance = subject.send "create_#{association_name}", association_attributes
70
- expect(created_association_instance).to be_an_instance_of(association_class)
71
- expect(created_association_instance).to_not be_new_record
31
+ context "#build_#{association_name}" do
32
+ let :association_instance_1 do subject.send "build_#{association_name}" end
33
+ let :association_instance_2 do subject.send "build_#{association_name}" end
34
+ it "should be a #{association_class.name}" do expect(association_instance_1).to be_a(association_class) end
35
+ it 'should not be a new record' do expect(association_instance_1).to be_new_record end
36
+ it "should be a #{association_class.name}" do expect(association_instance_2).to be_a(association_class) end
37
+ it 'should not be a new record' do expect(association_instance_2).to be_new_record end
38
+ it 'should not be identical' do expect(association_instance_1).to_not be(association_instance_2) end
72
39
  end
40
+
41
+ [ "create_#{association_name}", "create_#{association_name}!" ].each do |method_name|
42
+ context "##{method_name}" do
43
+ subject do described_class.create! model_attributes end
44
+ let :created_association_instance do subject.send method_name, association_attributes end
45
+ it "should be a #{association_class.name}" do expect(created_association_instance).to be_an_instance_of(association_class) end
46
+ it 'should not be a new record' do expect(created_association_instance).to_not be_new_record end
47
+ end
73
48
  end
74
- end
75
- =end
76
- =begin
77
- [ association_name, "#{association_name}=".to_sym, "build_#{association_name}".to_sym, "create_#{association_name}".to_sym ].each do |method|
78
- association_writable = #model_instance.reflections[association_name].class==::ActiveRecord::Reflection::AssociationReflection
79
- #model_instance._reflections[association_name].class==ActiveRecord::Reflection::AssociationReflection
80
- model_instance.association(association_name)._reflections.class==ActiveRecord::Reflection::AssociationReflection
81
- describe ".#{method}" do
82
49
 
83
- if association_writable
84
- it 'is responded to' do expect(model_instance).to respond_to(method.to_sym) end
85
- else
86
- unless [ "build_#{association_name}".to_sym, "create_#{association_name}".to_sym ].include? method
87
- it 'is responded to' do expect(model_instance).to respond_to(method.to_sym) end
88
- end
50
+ # Pending
51
+ =begin
52
+ context "#create_#{association_name}" do
53
+ subject do described_class.create! model_attributes end
54
+ it 'should return false with {}' do
55
+ expect { subject.send "create_#{association_name}", {} }.to be_falsey
89
56
  end
57
+ end
90
58
 
91
- case method
92
- when "build_#{association_name}".to_sym
93
- if association_writable
94
- it 'builds an association instance' do
95
- item_1 = model_instance.send method
96
- expect(item_1).to be_an_instance_of(association_class)
97
- expect(item_1).to be_new_record
98
- item_2 = model_instance.send method
99
- expect(item_2).to be_an_instance_of(association_class)
100
- expect(item_2).to be_new_record
101
- expect(item_1).to_not be(item_2)
102
- end
103
- end
104
- when "create_#{association_name}".to_sym
105
- if association_writable
106
- it 'creates an association instance' do
107
- pending 'Can not create association instance.'
108
- created_association_instance = model_instance.send method, association_attributes
109
- expect(created_association_instance).to be_an_instance_of(association_class)
110
- expect(created_association_instance).to_not be_new_record
111
- end
112
- end
113
- when "#{association_name}=".to_sym
114
- it 'assigns association successfully' do
115
- model_instance.send method, association_instance
116
- expect(model_instance.send association_name.to_sym).to eq(association_instance)
117
- end
59
+ context "#create_#{association_name}!" do
60
+ subject do described_class.create! model_attributes end
61
+ it 'should raise error with {}' do
62
+ expect { subject.send "create_#{association_name}!", {} }.to raise_error(ActiveRecord::RecordInvalid)
118
63
  end
119
64
  end
120
- end
121
65
  =end
122
66
 
67
+ end
68
+
123
69
  end
124
70
 
125
71
  end
@@ -1,7 +1,7 @@
1
1
  module Unidom
2
2
  module Common
3
3
  module RSpec
4
- VERSION = '0.6'
4
+ VERSION = '0.6.1'
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unidom-common-rspec
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.6'
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Topbit Du
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-01 00:00:00.000000000 Z
11
+ date: 2017-01-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec-rails