Skip to content

Commit

Permalink
Merge pull request #61 from bquorning/fix-===-for-multiple-stubs
Browse files Browse the repository at this point in the history
Fix === for multiple mocks of the same model
  • Loading branch information
JonRowe authored Oct 2, 2024
2 parents b6d57c8 + b299f07 commit 4142ad1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
### Development
[Full Changelog](https://github.com/rspec/rspec-activemodel-mocks/compare/v1.2.0...main)

Bug fixes:

* Fix `===` to work with multiple mocks. (@bquorning, #61)

### 1.2.0 / 2023-12-10
[Full Changelog](https://github.com/rspec/rspec-activemodel-mocks/compare/v1.1.0...v1.2.0)

Expand Down
5 changes: 4 additions & 1 deletion lib/rspec/active_model/mocks/mocks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def association(association_name)
# * A String representing a Class that extends ActiveModel::Naming
# * A Class that extends ActiveModel::Naming
def mock_model(string_or_model_class, stubs={})
@__rspec_active_model_mocks ||= Hash.new { |h, k| h[k] = [] }

if String === string_or_model_class
if Object.const_defined?(string_or_model_class)
model_class = Object.const_get(string_or_model_class)
Expand Down Expand Up @@ -137,9 +139,10 @@ def self.param_delimiter; "-"; end
double("#{model_class.name}_#{stubs[:id]}", stubs).tap do |m|
if model_class.method(:===).owner == Module && !stubs.key?(:===)
allow(model_class).to receive(:===).and_wrap_original do |original, other|
m === other || original.call(other)
@__rspec_active_model_mocks[model_class].include?(other) || original.call(other)
end
end
@__rspec_active_model_mocks[model_class] << m
msingleton = class << m; self; end
msingleton.class_eval do
include ActiveModelInstanceMethods
Expand Down
13 changes: 13 additions & 0 deletions spec/rspec/active_model/mocks/mock_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,19 @@ def self.===(_other)
end
# rubocop:enable Lint/LiteralAsCondition
end

it "works for multiple mocks of the same model" do
foo = mock_model(MockableModel)
bar = mock_model(MockableModel)
baz = mock_model(MockableModelNoPrimaryKey)
quz = mock_model(MockableModel)

expect(MockableModel === foo).to be(true)
expect(MockableModel === bar).to be(true)
expect(MockableModel === baz).to be(false)
expect(MockableModelNoPrimaryKey === baz).to be(true)
expect(MockableModel === quz).to be(true)
end
end

describe "#kind_of?" do
Expand Down
13 changes: 13 additions & 0 deletions spec/rspec/active_model/mocks/stub_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,18 @@ def model_class
end
# rubocop:enable Lint/LiteralAsCondition
end

it "works for multiple mocks of the same model" do
foo = stub_model(MockableModel)
bar = stub_model(MockableModel)
baz = stub_model(MockableModelNoPrimaryKey)
qux = stub_model(MockableModel)

expect(MockableModel === foo).to be(true)
expect(MockableModel === bar).to be(true)
expect(MockableModel === baz).to be(false)
expect(MockableModelNoPrimaryKey === baz).to be(true)
expect(MockableModel === qux).to be(true)
end
end
end

0 comments on commit 4142ad1

Please sign in to comment.