Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix === for multiple mocks of the same model #61

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading