unidom-common-rspec 0.5 → 0.6
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -1
- data/README.md +80 -11
- data/ROADMAP.md +4 -1
- data/lib/unidom/common/rspec.rb +4 -2
- data/lib/unidom/common/rspec/has_one_shared_examples.rb +125 -0
- data/lib/unidom/common/rspec/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9bc9d62357cf28e32abbdbcdac81fe842e6ba045
|
4
|
+
data.tar.gz: 65338e314cbb0633f8a586856472bcc27f7bbf07
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 439f897d6c939e3943ffc90e68808b8d2f0f571b50caa000590019e7ab4d8d727f629cec941e43567f424c0faf4561d5874c588313d6c9a96578f6434224c738
|
7
|
+
data.tar.gz: 0c54ac2b809fe7d97bab55a7e67fc004afa8b1687548c46dc46741da00923d4572fecde4de55437f0e7e9d4b6228005d9eaf13ba5d3ee96f6b5cf14171738141
|
data/CHANGELOG.md
CHANGED
@@ -14,5 +14,8 @@
|
|
14
14
|
|
15
15
|
## v0.5
|
16
16
|
1. Belongs To shared examples
|
17
|
-
2. Improve the Model Extension shared examples for the validates
|
17
|
+
2. Improve the Model Extension shared examples for the validates behaviors of the #grade attribute & the #priority attribute
|
18
18
|
3. Improve the Model Extension shared examples for the scope behaviours of the #priority attribute
|
19
|
+
|
20
|
+
## v0.6
|
21
|
+
1. Has One shared examples
|
data/README.md
CHANGED
@@ -35,9 +35,40 @@ 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:
|
39
|
+
```ruby
|
40
|
+
# person.rb
|
41
|
+
class Person < ApplicationRecord
|
42
|
+
|
43
|
+
include Unidom::Common::Concerns::ModelExtension
|
44
|
+
|
45
|
+
has_many :pets
|
46
|
+
has_one :identity_card
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
# pet.rb
|
51
|
+
class Pet < ApplicationRecord
|
52
|
+
|
53
|
+
include Unidom::Common::Concerns::ModelExtension
|
54
|
+
|
55
|
+
belongs_to :person
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
# identity_card.rb
|
60
|
+
class IdentityCard < ApplicationRecord
|
61
|
+
|
62
|
+
include Unidom::Common::Concerns::ModelExtension
|
63
|
+
|
64
|
+
belongs_to :person
|
65
|
+
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
38
69
|
### Scope shared examples Scope 共享用例
|
39
70
|
|
40
|
-
|
71
|
+
The ``person_spec.rb`` looks like the following:
|
41
72
|
If the ``count_diff`` is set to 'E', an error was expected to be raised.
|
42
73
|
```ruby
|
43
74
|
require 'rails_helper'
|
@@ -74,7 +105,7 @@ end
|
|
74
105
|
|
75
106
|
### Validates shared examples Validates 共享用例
|
76
107
|
|
77
|
-
|
108
|
+
The ``person_spec.rb`` looks like the following:
|
78
109
|
```ruby
|
79
110
|
require 'rails_helper'
|
80
111
|
|
@@ -99,9 +130,9 @@ describe Person, type: :model do
|
|
99
130
|
end
|
100
131
|
```
|
101
132
|
|
102
|
-
###
|
133
|
+
### Has Many shared examples Has Many 共享用例
|
103
134
|
|
104
|
-
|
135
|
+
The ``person_spec.rb`` looks like the following:
|
105
136
|
```ruby
|
106
137
|
require 'rails_helper'
|
107
138
|
|
@@ -110,17 +141,19 @@ describe Person, type: :model do
|
|
110
141
|
context do
|
111
142
|
|
112
143
|
tim_attributes = { name: 'Tim' }
|
144
|
+
cat_attributes = { name: 'Pearl', species: 'Persian' }
|
145
|
+
dog_attribtues = { name: 'Flower', species: 'Chihuahua' }
|
113
146
|
|
114
|
-
it_behaves_like '
|
147
|
+
it_behaves_like 'has_many', tim_attributes, :pets, Pet, [ cat_attributes, dog_attribtues ]
|
115
148
|
|
116
149
|
end
|
117
150
|
|
118
151
|
end
|
119
152
|
```
|
120
153
|
|
121
|
-
### Has
|
154
|
+
### Has One shared examples Has Many 共享用例
|
122
155
|
|
123
|
-
|
156
|
+
The ``person_spec.rb`` looks like the following:
|
124
157
|
```ruby
|
125
158
|
require 'rails_helper'
|
126
159
|
|
@@ -129,10 +162,9 @@ describe Person, type: :model do
|
|
129
162
|
context do
|
130
163
|
|
131
164
|
tim_attributes = { name: 'Tim' }
|
132
|
-
|
133
|
-
dog_attribtues = { name: 'Flower', species: 'Chihuahua' }
|
165
|
+
tim_identity_card_attributes = { name: 'Tim', gender_code: '1', birth_date: '1980-07-01' }
|
134
166
|
|
135
|
-
it_behaves_like '
|
167
|
+
it_behaves_like 'has_one', tim_attributes, :identity_card, IdentityCard, tim_identity_card_attributes
|
136
168
|
|
137
169
|
end
|
138
170
|
|
@@ -141,7 +173,7 @@ end
|
|
141
173
|
|
142
174
|
### Belongs To shared examples Belongs To 共享用例
|
143
175
|
|
144
|
-
|
176
|
+
The ``pet_spec.rb`` looks like the following:
|
145
177
|
```ruby
|
146
178
|
require 'rails_helper'
|
147
179
|
|
@@ -159,6 +191,43 @@ describe Pet, type: :model do
|
|
159
191
|
end
|
160
192
|
```
|
161
193
|
|
194
|
+
The ``identity_card_spec.rb`` looks like the following:
|
195
|
+
```ruby
|
196
|
+
require 'rails_helper'
|
197
|
+
|
198
|
+
describe IdentityCard, type: :model do
|
199
|
+
|
200
|
+
context do
|
201
|
+
|
202
|
+
tim_attributes = { name: 'Tim' }
|
203
|
+
tim_identity_card_attributes = { name: 'Tim', gender_code: '1', birth_date: '1980-07-01' }
|
204
|
+
|
205
|
+
it_behaves_like 'belongs_to', tim_identity_card_attributes, :person, Person, tim_attributes
|
206
|
+
|
207
|
+
end
|
208
|
+
|
209
|
+
end
|
210
|
+
```
|
211
|
+
|
212
|
+
### Model Extension shared examples Model Extension 共享用例
|
213
|
+
|
214
|
+
The ``person_spec.rb`` looks like the following:
|
215
|
+
```ruby
|
216
|
+
require 'rails_helper'
|
217
|
+
|
218
|
+
describe Person, type: :model do
|
219
|
+
|
220
|
+
context do
|
221
|
+
|
222
|
+
tim_attributes = { name: 'Tim' }
|
223
|
+
|
224
|
+
it_behaves_like 'Unidom::Common::Concerns::ModelExtension', tim_attributes
|
225
|
+
|
226
|
+
end
|
227
|
+
|
228
|
+
end
|
229
|
+
```
|
230
|
+
|
162
231
|
## Development
|
163
232
|
|
164
233
|
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
@@ -14,5 +14,8 @@
|
|
14
14
|
|
15
15
|
## v0.5
|
16
16
|
1. Belongs To shared examples
|
17
|
-
2. Improve the Model Extension shared examples for the validates
|
17
|
+
2. Improve the Model Extension shared examples for the validates behaviors of the #grade attribute & the #priority attribute
|
18
18
|
3. Improve the Model Extension shared examples for the scope behaviours of the #priority attribute
|
19
|
+
|
20
|
+
## v0.6
|
21
|
+
1. Has One shared examples
|
data/lib/unidom/common/rspec.rb
CHANGED
@@ -2,10 +2,12 @@ require "unidom/common/rspec/version"
|
|
2
2
|
|
3
3
|
require 'unidom/common/rspec/scope_shared_examples'
|
4
4
|
require 'unidom/common/rspec/validates_shared_examples'
|
5
|
-
require 'unidom/common/rspec/model_extension_shared_examples'
|
6
5
|
|
7
|
-
require 'unidom/common/rspec/has_many_shared_examples'
|
8
6
|
require 'unidom/common/rspec/belongs_to_shared_examples'
|
7
|
+
require 'unidom/common/rspec/has_many_shared_examples'
|
8
|
+
require 'unidom/common/rspec/has_one_shared_examples'
|
9
|
+
|
10
|
+
require 'unidom/common/rspec/model_extension_shared_examples'
|
9
11
|
|
10
12
|
module Unidom
|
11
13
|
module Common
|
@@ -0,0 +1,125 @@
|
|
1
|
+
shared_examples 'has_one' do |model_attributes, association_name, association_class, association_attributes|
|
2
|
+
|
3
|
+
describe "##{association_name}" do
|
4
|
+
|
5
|
+
model_instance = described_class.new model_attributes
|
6
|
+
association_instance = association_class.new association_attributes
|
7
|
+
reflection = model_instance.association(association_name.to_sym).reflection
|
8
|
+
association_writable = reflection.class==ActiveRecord::Reflection::HasOneReflection
|
9
|
+
gained_methods = [ association_name.to_sym ]
|
10
|
+
gained_methods << [ :"#{association_name}=", :"build_#{association_name}", :"create_#{association_name}", :"create_#{association_name}!" ] if association_writable
|
11
|
+
gained_methods.flatten!
|
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
|
17
|
+
|
18
|
+
before :each do
|
19
|
+
subject do
|
20
|
+
described_class.new model_attributes
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
after :each do
|
25
|
+
subject do
|
26
|
+
nil
|
27
|
+
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
|
33
|
+
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
|
+
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
|
+
|
67
|
+
expect(subject.save).to be_truthy
|
68
|
+
|
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
|
72
|
+
end
|
73
|
+
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
|
+
|
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
|
89
|
+
end
|
90
|
+
|
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
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
=end
|
122
|
+
|
123
|
+
end
|
124
|
+
|
125
|
+
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.
|
4
|
+
version: '0.6'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Topbit Du
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec-rails
|
@@ -89,6 +89,7 @@ files:
|
|
89
89
|
- lib/unidom/common/rspec.rb
|
90
90
|
- lib/unidom/common/rspec/belongs_to_shared_examples.rb
|
91
91
|
- lib/unidom/common/rspec/has_many_shared_examples.rb
|
92
|
+
- lib/unidom/common/rspec/has_one_shared_examples.rb
|
92
93
|
- lib/unidom/common/rspec/model_extension_shared_examples.rb
|
93
94
|
- lib/unidom/common/rspec/scope_shared_examples.rb
|
94
95
|
- lib/unidom/common/rspec/validates_shared_examples.rb
|