unidom-common-rspec 0.4 → 0.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a86129997899c61c37b4c47cbb46b955d88f4cdc
4
- data.tar.gz: 34c294a34e2dbf90b639831d2b4453428577a810
3
+ metadata.gz: 8f07873cd5ac82d966f96d653a6c9de1a89d7793
4
+ data.tar.gz: 27ad32bd93d045653bd0885bd2d5af69c9dcc74a
5
5
  SHA512:
6
- metadata.gz: ae6bf1b9a027504ca10f02aefe702266143ed0de82eabf073a93beefc95fee3a74bd675cad1cc0a0afcbeed3779115a8e2c77f00bd5a2ca1d00e7d56fbc62fff
7
- data.tar.gz: 16b681d61c787cf4aef24bf375a54a2f60acf627ac66340c4f8f7fa2d53f6bd7131718f4d9e04c6fd8f228b8a829f4b1be0e32e893744a86d46d3bb3d0b35031
6
+ metadata.gz: 5c3f631a5688b8785d74b0c5a0b7f13da43d9e8129ff8534f8a43b2fc2580f4050eec36fb7caec1d2c21603540f58f6f85226de44db31f508c9b81bb981498ca
7
+ data.tar.gz: 065ff70944668b6de3d7237c20c1eb68a1d525527a300eac879b8ee5b5f3e6af39b77d3fe2abc40ecbb979a1b1c9dfecb2a5381eb4ce84d26a1829a17a8876b6
data/CHANGELOG.md CHANGED
@@ -11,3 +11,8 @@
11
11
 
12
12
  ## v0.4
13
13
  1. Has Many shared examples
14
+
15
+ ## v0.5
16
+ 1. Belongs To shared examples
17
+ 2. Improve the Model Extension shared examples for the validates behaviours of the #grade attribute & the #priority attribute
18
+ 3. Improve the Model Extension shared examples for the scope behaviours of the #priority attribute
data/README.md CHANGED
@@ -120,7 +120,7 @@ end
120
120
 
121
121
  ### Has Many shared examples Has Many 共享用例
122
122
 
123
- Assume the model class is ``Person``, and the model alread defined the ``has_many :pets``, the ``person_spec.rb`` looks like the following:
123
+ Assume the model class is ``Person``, and the model already defined the ``has_many :pets``, the ``person_spec.rb`` looks like the following:
124
124
  ```ruby
125
125
  require 'rails_helper'
126
126
 
@@ -139,6 +139,26 @@ describe Person, type: :model do
139
139
  end
140
140
  ```
141
141
 
142
+ ### Belongs To shared examples Belongs To 共享用例
143
+
144
+ Assume the model class is ``Pet``, and the model already defined the ``belongs_to :person``, the ``pet_spec.rb`` looks like the following:
145
+ ```ruby
146
+ require 'rails_helper'
147
+
148
+ describe Pet, type: :model do
149
+
150
+ context do
151
+
152
+ cat_attributes = { name: 'Pearl', species: 'Persian' }
153
+ tim_attributes = { name: 'Tim' }
154
+
155
+ it_behaves_like 'belongs_to', cat_attributes, :person, Person, tim_attributes
156
+
157
+ end
158
+
159
+ end
160
+ ```
161
+
142
162
  ## Development
143
163
 
144
164
  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
@@ -11,3 +11,8 @@
11
11
 
12
12
  ## v0.4
13
13
  1. Has Many shared examples
14
+
15
+ ## v0.5
16
+ 1. Belongs To shared examples
17
+ 2. Improve the Model Extension shared examples for the validates behaviours of the #grade attribute & the #priority attribute
18
+ 3. Improve the Model Extension shared examples for the scope behaviours of the #priority attribute
@@ -5,6 +5,7 @@ require 'unidom/common/rspec/validates_shared_examples'
5
5
  require 'unidom/common/rspec/model_extension_shared_examples'
6
6
 
7
7
  require 'unidom/common/rspec/has_many_shared_examples'
8
+ require 'unidom/common/rspec/belongs_to_shared_examples'
8
9
 
9
10
  module Unidom
10
11
  module Common
@@ -0,0 +1,38 @@
1
+ shared_examples 'belongs_to' do |model_attributes, association_name, association_class, association_attributes|
2
+
3
+ describe "##{association_name}: #{association_class.name}" do
4
+
5
+ model_instance = described_class.new model_attributes
6
+ association_instance = association_class.new association_attributes
7
+ association = model_instance.association association_name.to_sym
8
+ association_writable = association.class==ActiveRecord::Associations::BelongsToAssociation
9
+
10
+ methods = [ association_name, "#{association_name}=" ]
11
+ methods = methods+[ "build_#{association_name}", "create_#{association_name}", "create_#{association_name}!" ] if association_writable
12
+
13
+ methods.each do |method|
14
+ describe "##{method}" do
15
+
16
+ it 'is responded to' do expect(model_instance).to respond_to(association_name.to_sym) end
17
+
18
+ case method
19
+
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
25
+
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
32
+
33
+ end
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -200,11 +200,11 @@ shared_examples 'Unidom::Common::Concerns::ModelExtension' do |model_attributes|
200
200
  if grade_column.present?&&:integer==grade_column.type
201
201
 
202
202
  it_behaves_like 'validates', model_attributes, :grade,
203
- { } => 3,
204
- { grade: nil } => 3,
205
- { grade: '' } => 3,
206
- { grade: 'A' } => 2,
207
- { grade: 'AA' } => 2,
203
+ #{ } => 3,
204
+ { grade: nil } => 2,
205
+ { grade: '' } => 2,
206
+ { grade: 'A' } => 1,
207
+ { grade: 'AA' } => 1,
208
208
  { grade: -1 } => 1,
209
209
  { grade: -11 } => 1,
210
210
  { grade: 1.1 } => 1,
@@ -242,11 +242,11 @@ shared_examples 'Unidom::Common::Concerns::ModelExtension' do |model_attributes|
242
242
  if priority_column.present?&&:integer==priority_column.type
243
243
 
244
244
  it_behaves_like 'validates', model_attributes, :priority,
245
- { } => 3,
246
- { priority: nil } => 3,
247
- { priority: '' } => 3,
248
- { priority: 'A' } => 2,
249
- { priority: 'AA' } => 2,
245
+ #{ } => 3,
246
+ { priority: nil } => 2,
247
+ { priority: '' } => 2,
248
+ { priority: 'A' } => 1,
249
+ { priority: 'AA' } => 1,
250
250
  { priority: -1 } => 1,
251
251
  { priority: -11 } => 1,
252
252
  { priority: 1.1 } => 1,
@@ -257,19 +257,19 @@ shared_examples 'Unidom::Common::Concerns::ModelExtension' do |model_attributes|
257
257
  { priority: 1 } => 0,
258
258
  { priority: 11 } => 0
259
259
 
260
- it_behaves_like 'scope', :grade_is, [
260
+ it_behaves_like 'scope', :priority_is, [
261
261
  { attributes_collection: [ model_attributes ], count_diff: 0, args: [ model_attributes[:priority]-1 ] },
262
262
  { attributes_collection: [ model_attributes ], count_diff: 1, args: [ model_attributes[:priority] ] },
263
263
  { attributes_collection: [ model_attributes ], count_diff: 0, args: [ model_attributes[:priority]+1 ] }
264
264
  ]
265
265
 
266
- it_behaves_like 'scope', :grade_higher_than, [
266
+ it_behaves_like 'scope', :priority_higher_than, [
267
267
  { attributes_collection: [ model_attributes ], count_diff: 1, args: [ model_attributes[:priority]-1 ] },
268
268
  { attributes_collection: [ model_attributes ], count_diff: 0, args: [ model_attributes[:priority] ] },
269
269
  { attributes_collection: [ model_attributes ], count_diff: 0, args: [ model_attributes[:priority]+1 ] }
270
270
  ]
271
271
 
272
- it_behaves_like 'scope', :grade_lower_than, [
272
+ it_behaves_like 'scope', :priority_lower_than, [
273
273
  { attributes_collection: [ model_attributes ], count_diff: 0, args: [ model_attributes[:priority]-1 ] },
274
274
  { attributes_collection: [ model_attributes ], count_diff: 0, args: [ model_attributes[:priority] ] },
275
275
  { attributes_collection: [ model_attributes ], count_diff: 1, args: [ model_attributes[:priority]+1 ] }
@@ -1,7 +1,7 @@
1
1
  module Unidom
2
2
  module Common
3
3
  module RSpec
4
- VERSION = '0.4'
4
+ VERSION = '0.5'
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.4'
4
+ version: '0.5'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Topbit Du
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-28 00:00:00.000000000 Z
11
+ date: 2016-12-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec-rails
@@ -87,6 +87,7 @@ files:
87
87
  - bin/console
88
88
  - bin/setup
89
89
  - lib/unidom/common/rspec.rb
90
+ - lib/unidom/common/rspec/belongs_to_shared_examples.rb
90
91
  - lib/unidom/common/rspec/has_many_shared_examples.rb
91
92
  - lib/unidom/common/rspec/model_extension_shared_examples.rb
92
93
  - lib/unidom/common/rspec/scope_shared_examples.rb