unidom-common-rspec 0.3 → 0.4

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: 423975fbbd2d45be90dad54b10bb33d9fca86ca3
4
- data.tar.gz: 972f708c3a3cd7ce84ce54dca26b7db5ac5d5f82
3
+ metadata.gz: a86129997899c61c37b4c47cbb46b955d88f4cdc
4
+ data.tar.gz: 34c294a34e2dbf90b639831d2b4453428577a810
5
5
  SHA512:
6
- metadata.gz: fe7a08d3627566465e51b205c9936bfa8bb5b9c7523c9a6b100c9243d2a4e36c7a9f2e93c9e051d2e3825060f663e18fa7dd57d8ba92a8ec10d0350e2d52701e
7
- data.tar.gz: aae6459986887bfb5d5002d0c42363910f7ffb86de2ae1dd52ffbce38c814002ded13906ecd42af120d778371c4996bd1c144e4687ec14c47c7079e5e00492ec
6
+ metadata.gz: ae6bf1b9a027504ca10f02aefe702266143ed0de82eabf073a93beefc95fee3a74bd675cad1cc0a0afcbeed3779115a8e2c77f00bd5a2ca1d00e7d56fbc62fff
7
+ data.tar.gz: 16b681d61c787cf4aef24bf375a54a2f60acf627ac66340c4f8f7fa2d53f6bd7131718f4d9e04c6fd8f228b8a829f4b1be0e32e893744a86d46d3bb3d0b35031
data/CHANGELOG.md CHANGED
@@ -8,3 +8,6 @@
8
8
 
9
9
  ## v0.3
10
10
  1. Model Extension shared examples
11
+
12
+ ## v0.4
13
+ 1. Has Many shared examples
data/README.md CHANGED
@@ -111,7 +111,28 @@ describe Person, type: :model do
111
111
 
112
112
  tim_attributes = { name: 'Tim' }
113
113
 
114
- it_behaves_like 'Unidom::Common::Concerns::ModelExtension', model_attributes
114
+ it_behaves_like 'Unidom::Common::Concerns::ModelExtension', tim_attributes
115
+
116
+ end
117
+
118
+ end
119
+ ```
120
+
121
+ ### Has Many shared examples Has Many 共享用例
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:
124
+ ```ruby
125
+ require 'rails_helper'
126
+
127
+ describe Person, type: :model do
128
+
129
+ context do
130
+
131
+ tim_attributes = { name: 'Tim' }
132
+ cat_attributes = { name: 'Pearl', species: 'Persian' }
133
+ dog_attribtues = { name: 'Flower', species: 'Chihuahua' }
134
+
135
+ it_behaves_like 'has_many', tim_attributes, :pets, 'Pet', [ cat_attributes, dog_attribtues ]
115
136
 
116
137
  end
117
138
 
data/ROADMAP.md CHANGED
@@ -8,3 +8,6 @@
8
8
 
9
9
  ## v0.3
10
10
  1. Model Extension shared examples
11
+
12
+ ## v0.4
13
+ 1. Has Many shared examples
@@ -4,10 +4,11 @@ require 'unidom/common/rspec/scope_shared_examples'
4
4
  require 'unidom/common/rspec/validates_shared_examples'
5
5
  require 'unidom/common/rspec/model_extension_shared_examples'
6
6
 
7
+ require 'unidom/common/rspec/has_many_shared_examples'
8
+
7
9
  module Unidom
8
10
  module Common
9
11
  module RSpec
10
- # Your code goes here...
11
12
  end
12
13
  end
13
14
  end
@@ -0,0 +1,81 @@
1
+ shared_examples 'has_many' do |model_attributes, collection_name, item_class, item_attributes_collection|
2
+
3
+ describe "##{collection_name}" do
4
+
5
+ model_instance = described_class.new model_attributes
6
+ collection = model_instance.send collection_name.to_sym
7
+ item_instances = item_attributes_collection.map do |item_attributes| item_class.new item_attributes end
8
+ macro_definition = model_instance.association collection_name.to_sym
9
+ being_through = macro_definition.is_a? ActiveRecord::Associations::ThroughAssociation
10
+ reflection = macro_definition.reflection
11
+ association_readonly = reflection.nested?
12
+ #association_immutable = reflection.source_reflection.blank? ? false : (:belongs_to != reflection.source_reflection.macro)
13
+ association_immutable = reflection.source_reflection.blank? ? false : being_through&&![ :belongs_to ].include?(reflection.source_reflection.macro)
14
+
15
+ [ collection_name, "#{collection_name}=", "#{collection_name.to_s.singularize}_ids", "#{collection_name.to_s.singularize}_ids=" ].each do |method|
16
+ describe "##{method}" do
17
+ it 'is responded to' do expect(model_instance).to respond_to(method.to_sym) end
18
+ end
19
+ end
20
+
21
+ %w{ << push concat build create create! size length count sum where empty? clear delete delete_all destroy destroy_all find exists? uniq reset }.each do |method|
22
+
23
+ describe "##{method}" do
24
+
25
+ before :each do collection.clear if collection.present? end
26
+ after :each do collection.clear if collection.present? end
27
+
28
+ it 'is responded to' do expect(collection).to respond_to(method.to_sym) end
29
+
30
+ case method
31
+
32
+ when '<<'
33
+ if association_readonly || association_immutable
34
+ #if association_immutable
35
+ it 'nested association can not build' do
36
+ error_class = association_readonly ? ActiveRecord::HasManyThroughNestedAssociationsAreReadonly : ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection
37
+ expect { item_instances.each do |item_instance| collection << item_instance end }.to raise_error(error_class)
38
+ end
39
+ else
40
+ it 'adds items' do
41
+ expect(collection.size).to eq(0)
42
+ item_instances.each do |item_instance| collection << item_instance end
43
+ item_instances.each_index do |index| expect(collection[index]).to eq(item_instances[index]) end
44
+ expect(collection.size).to eq(item_instances.size)
45
+ end
46
+ end
47
+
48
+ when 'build'
49
+ if association_readonly #or association_immutable
50
+ it 'nested association can not build' do
51
+ error_class = association_readonly ? ActiveRecord::HasManyThroughNestedAssociationsAreReadonly : ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection
52
+ expect { collection.build }.to raise_error(error_class)
53
+ end
54
+ else
55
+ it 'builds an item instance' do
56
+ item_1 = collection.build
57
+ expect(item_1).to be_an_instance_of(item_class)
58
+ expect(item_1).to be_new_record
59
+ item_2 = collection.build
60
+ expect(item_2).to be_an_instance_of(item_class)
61
+ expect(item_2).to be_new_record
62
+ expect(item_1).to_not be(item_2)
63
+ end
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+ end
70
+
71
+ # The following method(s) does not exist. Who know why?
72
+ %w{}.each do |method|
73
+ it "##{method}" do
74
+ pending ":#{method} method does not exist."
75
+ expect(collection).to respond_to(method.to_sym)
76
+ end
77
+ end
78
+
79
+ end
80
+
81
+ end
@@ -1,7 +1,7 @@
1
1
  module Unidom
2
2
  module Common
3
3
  module RSpec
4
- VERSION = '0.3'
4
+ VERSION = '0.4'
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.3'
4
+ version: '0.4'
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-27 00:00:00.000000000 Z
11
+ date: 2016-12-28 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/has_many_shared_examples.rb
90
91
  - lib/unidom/common/rspec/model_extension_shared_examples.rb
91
92
  - lib/unidom/common/rspec/scope_shared_examples.rb
92
93
  - lib/unidom/common/rspec/validates_shared_examples.rb