Skip to content

Commit 6fcf4d8

Browse files
committed
Add support for active record read_attribute public method
1 parent 9c303c2 commit 6fcf4d8

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lib/rspec/active_model/mocks/mocks.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ def [](key)
6161
# alternative to record['id']
6262
alias_method :_read_attribute, :[]
6363

64+
# Rails>7.1 added read_attribute for external usage similar to record['id']
65+
alias_method :read_attribute, :[]
66+
6467
# Returns the opposite of `persisted?`
6568
def new_record?
6669
!persisted?

spec/rspec/active_model/mocks/mock_model_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,44 @@
415415
end
416416
end
417417

418+
describe "attribute access methods" do
419+
it "supports [] method with symbol" do
420+
model = mock_model(MockableModel, :name => "John", :age => 25)
421+
expect(model[:name]).to eq("John")
422+
expect(model[:age]).to eq(25)
423+
end
424+
425+
it "supports [] method with string" do
426+
model = mock_model(MockableModel, :name => "John", :age => 25)
427+
expect(model["name"]).to eq("John")
428+
expect(model["age"]).to eq(25)
429+
end
430+
431+
it "supports read_attribute method with symbol" do
432+
model = mock_model(MockableModel, :name => "John", :age => 25)
433+
expect(model.read_attribute(:name)).to eq("John")
434+
expect(model.read_attribute(:age)).to eq(25)
435+
end
436+
437+
it "supports read_attribute method with string" do
438+
model = mock_model(MockableModel, :name => "John", :age => 25)
439+
expect(model.read_attribute("name")).to eq("John")
440+
expect(model.read_attribute("age")).to eq(25)
441+
end
442+
443+
it "supports _read_attribute method with symbol" do
444+
model = mock_model(MockableModel, :name => "John", :age => 25)
445+
expect(model._read_attribute(:name)).to eq("John")
446+
expect(model._read_attribute(:age)).to eq(25)
447+
end
448+
449+
it "supports _read_attribute method with string" do
450+
model = mock_model(MockableModel, :name => "John", :age => 25)
451+
expect(model._read_attribute("name")).to eq("John")
452+
expect(model._read_attribute("age")).to eq(25)
453+
end
454+
end
455+
418456
describe "ActiveModel Lint tests" do
419457
begin
420458
require 'minitest/assertions'

0 commit comments

Comments
 (0)