Skip to content
Closed
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
5 changes: 5 additions & 0 deletions lib/rspec/active_model/mocks/mocks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ def [](key)
# alternative to record['id']
alias_method :_read_attribute, :[]

# Rails>7.1 added read_attribute for external usage similar to record['id']
alias_method :read_attribute, :[]

# Returns the opposite of `persisted?`
def new_record?
!persisted?
Expand Down Expand Up @@ -103,6 +106,7 @@ def mock_model(string_or_model_class, stubs={})
model_class = Object.const_set(string_or_model_class, Class.new do
# rubocop:disable Style/SingleLineMethods
extend ::ActiveModel::Naming

def self.primary_key; :id; end

# For detection of being a valid association in 7+
Expand Down Expand Up @@ -146,6 +150,7 @@ def self.param_delimiter; "-"; end
msingleton = class << m; self; end
msingleton.class_eval do
include ActiveModelInstanceMethods

include ActiveRecordInstanceMethods if defined?(ActiveRecord)
include ActiveModel::Conversion
include ActiveModel::Validations
Expand Down
40 changes: 40 additions & 0 deletions spec/rspec/active_model/mocks/mock_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,44 @@ def self.===(_other)
end
end

describe "attribute access methods" do
it "supports [] method with symbol" do
model = mock_model(MockableModel, :name => "John", :age => 25)
expect(model[:name]).to eq("John")
expect(model[:age]).to eq(25)
end

it "supports [] method with string" do
model = mock_model(MockableModel, :name => "John", :age => 25)
expect(model["name"]).to eq("John")
expect(model["age"]).to eq(25)
end

it "supports read_attribute method with symbol" do
model = mock_model(MockableModel, :name => "John", :age => 25)
expect(model.read_attribute(:name)).to eq("John")
expect(model.read_attribute(:age)).to eq(25)
end

it "supports read_attribute method with string" do
model = mock_model(MockableModel, :name => "John", :age => 25)
expect(model.read_attribute("name")).to eq("John")
expect(model.read_attribute("age")).to eq(25)
end

it "supports _read_attribute method with symbol" do
model = mock_model(MockableModel, :name => "John", :age => 25)
expect(model._read_attribute(:name)).to eq("John")
expect(model._read_attribute(:age)).to eq(25)
end

it "supports _read_attribute method with string" do
model = mock_model(MockableModel, :name => "John", :age => 25)
expect(model._read_attribute("name")).to eq("John")
expect(model._read_attribute("age")).to eq(25)
end
end

describe "ActiveModel Lint tests" do
# rubocop:disable Lint/EmptyExpression,Metrics/BlockNesting
begin
Expand Down Expand Up @@ -519,6 +557,7 @@ def self.===(_other)
ERR
end
include Test::Unit::Assertions

if defined?((Test::Unit::AutoRunner.need_auto_run = ()))
Test::Unit::AutoRunner.need_auto_run = false
elsif defined?((Test::Unit.run = ()))
Expand All @@ -532,6 +571,7 @@ def self.===(_other)
else
require 'test/unit/assertions'
include Test::Unit::Assertions

if defined?((Test::Unit::AutoRunner.need_auto_run = ()))
Test::Unit::AutoRunner.need_auto_run = false
elsif defined?((Test::Unit.run = ()))
Expand Down
3 changes: 3 additions & 0 deletions spec/support/ar_classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class NonActiveRecordModel

class MockableModel < ActiveRecord::Base
extend Connections

has_one :associated_model
end

Expand All @@ -54,12 +55,14 @@ class SubMockableModel < MockableModel

class AssociatedModel < ActiveRecord::Base
extend Connections

belongs_to :mockable_model
belongs_to :nonexistent_model, :class_name => "Other"
end

class AlternatePrimaryKeyModel < ActiveRecord::Base
self.primary_key = :my_id
extend Connections

attr_accessor :my_id
end