rspec-activemodel-mocks 1.2.0 → 1.3.0

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
  SHA256:
3
- metadata.gz: 7c03088fe7611777d052655997f79e7394142634a18c47f12ef2b62d21c1225d
4
- data.tar.gz: 59d467700bd06b7246d40282f73bf9f374574c951455f3400e7ec47e3e0dedc5
3
+ metadata.gz: a94dd2b6d61f1431637636ffc999a67abdd92e70d51de9daea7844fb4e706c7d
4
+ data.tar.gz: 6f5ee85b061eec9923b3d29c9256b3a0ad0eb38e6cc5b5b23c04ac2f51c849ee
5
5
  SHA512:
6
- metadata.gz: 19ddf1bdf625c89ae9908c504af7ba69204d9e07dcaffddf7baafc4401fcd5b90a1c0e9b747b63be4bf05e10017976dd63a9c3c435626344469a05542d401282
7
- data.tar.gz: fddd4cf43f973358d3b04762aaedebcc629a7e714bbcf0e82f13f875e6fb6f5c3eb2bf4b328c7227ea55c1b03e12e047420b92463277e47e5a284f551eabfad6
6
+ metadata.gz: 209174dc5f015e1b1b9b80061659875032802ee616365534381df1c9bf7d7abdaa1c0a6aaba58499ab957216d270b8aae2e7efaf5e8e9a6ab6251724f41fb191
7
+ data.tar.gz: 59637b95cc2ba2fc1fc303b4df514a979e6945d8b0cfdfc120db91153b245aba6048e14b87415f79c75a79faba08086fe3b25f7ec74b10bda9ecb7fdb9fbd3c6
checksums.yaml.gz.sig CHANGED
Binary file
@@ -61,6 +61,9 @@ module RSpec::ActiveModel::Mocks
61
61
  # alternative to record['id']
62
62
  alias_method :_read_attribute, :[]
63
63
 
64
+ # Rails>7.1 added read_attribute for external usage similar to record['id']
65
+ alias_method :read_attribute, :[]
66
+
64
67
  # Returns the opposite of `persisted?`
65
68
  def new_record?
66
69
  !persisted?
@@ -94,6 +97,8 @@ module RSpec::ActiveModel::Mocks
94
97
  # * A String representing a Class that extends ActiveModel::Naming
95
98
  # * A Class that extends ActiveModel::Naming
96
99
  def mock_model(string_or_model_class, stubs={})
100
+ @__rspec_active_model_mocks ||= Hash.new { |h, k| h[k] = [] }
101
+
97
102
  if String === string_or_model_class
98
103
  if Object.const_defined?(string_or_model_class)
99
104
  model_class = Object.const_get(string_or_model_class)
@@ -101,6 +106,7 @@ module RSpec::ActiveModel::Mocks
101
106
  model_class = Object.const_set(string_or_model_class, Class.new do
102
107
  # rubocop:disable Style/SingleLineMethods
103
108
  extend ::ActiveModel::Naming
109
+
104
110
  def self.primary_key; :id; end
105
111
 
106
112
  # For detection of being a valid association in 7+
@@ -137,9 +143,10 @@ It received #{model_class.inspect}
137
143
  double("#{model_class.name}_#{stubs[:id]}", stubs).tap do |m|
138
144
  if model_class.method(:===).owner == Module && !stubs.key?(:===)
139
145
  allow(model_class).to receive(:===).and_wrap_original do |original, other|
140
- m === other || original.call(other)
146
+ @__rspec_active_model_mocks[model_class].include?(other) || original.call(other)
141
147
  end
142
148
  end
149
+ @__rspec_active_model_mocks[model_class] << m
143
150
  msingleton = class << m; self; end
144
151
  msingleton.class_eval do
145
152
  include ActiveModelInstanceMethods
@@ -173,8 +180,11 @@ It received #{model_class.inspect}
173
180
  end unless stubs.key?(:has_attribute?)
174
181
 
175
182
  msingleton.__send__(:define_method, :respond_to?) do |method_name, *args|
183
+ return true if __model_class_has_column?(method_name)
184
+
176
185
  include_private = args.first || false
177
- __model_class_has_column?(method_name) ? true : super(method_name, include_private)
186
+
187
+ super(method_name, include_private)
178
188
  end unless stubs.key?(:respond_to?)
179
189
 
180
190
  msingleton.__send__(:define_method, :method_missing) do |missing_m, *a, &b|
@@ -3,7 +3,7 @@ module RSpec
3
3
  module ActiveModel
4
4
  module Mocks
5
5
  module Version
6
- STRING = '1.2.0'
6
+ STRING = '1.3.0'
7
7
  end
8
8
  end
9
9
  end
@@ -209,6 +209,19 @@ describe "mock_model(RealModel)" do
209
209
  end
210
210
  # rubocop:enable Lint/LiteralAsCondition
211
211
  end
212
+
213
+ it "works for multiple mocks of the same model" do
214
+ foo = mock_model(MockableModel)
215
+ bar = mock_model(MockableModel)
216
+ baz = mock_model(MockableModelNoPrimaryKey)
217
+ quz = mock_model(MockableModel)
218
+
219
+ expect(MockableModel === foo).to be(true)
220
+ expect(MockableModel === bar).to be(true)
221
+ expect(MockableModel === baz).to be(false)
222
+ expect(MockableModelNoPrimaryKey === baz).to be(true)
223
+ expect(MockableModel === quz).to be(true)
224
+ end
212
225
  end
213
226
 
214
227
  describe "#kind_of?" do
@@ -469,6 +482,44 @@ describe "mock_model(RealModel)" do
469
482
  end
470
483
  end
471
484
 
485
+ describe "attribute access methods" do
486
+ it "supports [] method with symbol" do
487
+ model = mock_model(MockableModel, :name => "John", :age => 25)
488
+ expect(model[:name]).to eq("John")
489
+ expect(model[:age]).to eq(25)
490
+ end
491
+
492
+ it "supports [] method with string" do
493
+ model = mock_model(MockableModel, :name => "John", :age => 25)
494
+ expect(model["name"]).to eq("John")
495
+ expect(model["age"]).to eq(25)
496
+ end
497
+
498
+ it "supports read_attribute method with symbol" do
499
+ model = mock_model(MockableModel, :name => "John", :age => 25)
500
+ expect(model.read_attribute(:name)).to eq("John")
501
+ expect(model.read_attribute(:age)).to eq(25)
502
+ end
503
+
504
+ it "supports read_attribute method with string" do
505
+ model = mock_model(MockableModel, :name => "John", :age => 25)
506
+ expect(model.read_attribute("name")).to eq("John")
507
+ expect(model.read_attribute("age")).to eq(25)
508
+ end
509
+
510
+ it "supports _read_attribute method with symbol" do
511
+ model = mock_model(MockableModel, :name => "John", :age => 25)
512
+ expect(model._read_attribute(:name)).to eq("John")
513
+ expect(model._read_attribute(:age)).to eq(25)
514
+ end
515
+
516
+ it "supports _read_attribute method with string" do
517
+ model = mock_model(MockableModel, :name => "John", :age => 25)
518
+ expect(model._read_attribute("name")).to eq("John")
519
+ expect(model._read_attribute("age")).to eq(25)
520
+ end
521
+ end
522
+
472
523
  describe "ActiveModel Lint tests" do
473
524
  # rubocop:disable Lint/EmptyExpression,Metrics/BlockNesting
474
525
  begin
@@ -506,6 +557,7 @@ describe "mock_model(RealModel)" do
506
557
  ERR
507
558
  end
508
559
  include Test::Unit::Assertions
560
+
509
561
  if defined?((Test::Unit::AutoRunner.need_auto_run = ()))
510
562
  Test::Unit::AutoRunner.need_auto_run = false
511
563
  elsif defined?((Test::Unit.run = ()))
@@ -519,6 +571,7 @@ describe "mock_model(RealModel)" do
519
571
  else
520
572
  require 'test/unit/assertions'
521
573
  include Test::Unit::Assertions
574
+
522
575
  if defined?((Test::Unit::AutoRunner.need_auto_run = ()))
523
576
  Test::Unit::AutoRunner.need_auto_run = false
524
577
  elsif defined?((Test::Unit.run = ()))
@@ -181,5 +181,18 @@ describe "stub_model" do
181
181
  end
182
182
  # rubocop:enable Lint/LiteralAsCondition
183
183
  end
184
+
185
+ it "works for multiple mocks of the same model" do
186
+ foo = stub_model(MockableModel)
187
+ bar = stub_model(MockableModel)
188
+ baz = stub_model(MockableModelNoPrimaryKey)
189
+ qux = stub_model(MockableModel)
190
+
191
+ expect(MockableModel === foo).to be(true)
192
+ expect(MockableModel === bar).to be(true)
193
+ expect(MockableModel === baz).to be(false)
194
+ expect(MockableModelNoPrimaryKey === baz).to be(true)
195
+ expect(MockableModel === qux).to be(true)
196
+ end
184
197
  end
185
198
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ require 'logger'
2
3
  require 'rspec/active_model/mocks'
3
4
  require 'active_record'
4
5
 
@@ -41,6 +41,7 @@ end
41
41
 
42
42
  class MockableModel < ActiveRecord::Base
43
43
  extend Connections
44
+
44
45
  has_one :associated_model
45
46
  end
46
47
 
@@ -54,6 +55,7 @@ end
54
55
 
55
56
  class AssociatedModel < ActiveRecord::Base
56
57
  extend Connections
58
+
57
59
  belongs_to :mockable_model
58
60
  belongs_to :nonexistent_model, :class_name => "Other"
59
61
  end
@@ -61,5 +63,6 @@ end
61
63
  class AlternatePrimaryKeyModel < ActiveRecord::Base
62
64
  self.primary_key = :my_id
63
65
  extend Connections
66
+
64
67
  attr_accessor :my_id
65
68
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,22 +1,21 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-activemodel-mocks
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Chelimsky
8
8
  - Andy Lindeman
9
9
  - Thomas Holmes
10
- autorequire:
11
10
  bindir: bin
12
11
  cert_chain:
13
12
  - |
14
13
  -----BEGIN CERTIFICATE-----
15
- MIIF1TCCA72gAwIBAgIJAPXjfUbCjdXUMA0GCSqGSIb3DQEBBQUAMIGAMQswCQYD
14
+ MIIFvjCCA6agAwIBAgIJAPXjfUbCjdXVMA0GCSqGSIb3DQEBCwUAMIGAMQswCQYD
16
15
  VQQGEwJVUzETMBEGA1UECAwKV2FzaGluZ3RvbjEQMA4GA1UEBwwHU2VhdHRsZTEO
17
16
  MAwGA1UECgwFUlNwZWMxEzARBgNVBAMMCnJzcGVjLmluZm8xJTAjBgkqhkiG9w0B
18
- CQEWFnJzcGVjQGdvb2dsZWdyb3Vwcy5jb20wHhcNMTQxMjIzMDkzNTIyWhcNMjQx
19
- MjIyMDkzNTIyWjCBgDELMAkGA1UEBhMCVVMxEzARBgNVBAgMCldhc2hpbmd0b24x
17
+ CQEWFnJzcGVjQGdvb2dsZWdyb3Vwcy5jb20wHhcNMjUwMjA2MTE0NjU2WhcNMjYw
18
+ MjA2MTE0NjU2WjCBgDELMAkGA1UEBhMCVVMxEzARBgNVBAgMCldhc2hpbmd0b24x
20
19
  EDAOBgNVBAcMB1NlYXR0bGUxDjAMBgNVBAoMBVJTcGVjMRMwEQYDVQQDDApyc3Bl
21
20
  Yy5pbmZvMSUwIwYJKoZIhvcNAQkBFhZyc3BlY0Bnb29nbGVncm91cHMuY29tMIIC
22
21
  IjANBgkqhkiG9w0BAQEFAAOCA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.000000000 Z
49
47
  dependencies:
50
48
  - !ruby/object:Gem::Dependency
51
49
  name: activemodel
@@ -130,7 +128,6 @@ metadata:
130
128
  mailing_list_uri: https://groups.google.com/forum/#!forum/rspec
131
129
  source_code_uri: https://github.com/rspec/rspec-activemodel-mocks
132
130
  rubygems_mfa_required: 'true'
133
- post_install_message:
134
131
  rdoc_options:
135
132
  - "--charset=UTF-8"
136
133
  require_paths:
@@ -146,10 +143,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
143
  - !ruby/object:Gem::Version
147
144
  version: '0'
148
145
  requirements: []
149
- rubygems_version: 3.4.10
150
- signing_key:
146
+ rubygems_version: 3.6.7
151
147
  specification_version: 4
152
- summary: rspec-activemodel-mocks-1.2.0
148
+ summary: rspec-activemodel-mocks-1.3.0
153
149
  test_files:
154
150
  - features/mocks/mock_model.feature
155
151
  - features/mocks/stub_model.feature
metadata.gz.sig CHANGED
Binary file