rspec / rspec/rspec-activemodel-mocks

`readonly?` query method

Open
#47 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
119
Forks
31
Avg merge
5h 47m
Merged PRs (30d)
2

Description

To more fully mock an ActiveRecord object should the mock respond to the readonly? method (similar to the `new_record? query method)?

Use case. We sometimes have a object that enters a final state. To enforce that the record is not updated after that final state we override readonly? to examine the object state to determine if it should be read only or nor. For example:

def readonly?
  super || state == 'complete'
end

Our UI then reads that attribute to know of it needs to present the option to edit that record (via a Pundit policy but that is an impl detail not relevant).

If I use a real ActiveRecord object this all works flawless because readonly? is a built-in method. But if I use a mock from this library that is supposed to be able to replace an ActiveRecord object it does not work.

Seems some way for it to respond to readonly? and return false (mirroring a ActiveRecord object by default) would be good. Bonus points if there was a good way to change the return value for a specific test although that's not a huge need of mine currently.

Wanted to inquire if this was in-scope for this project or just something I need to handle internally (by further mocking what this library returns to more closely mirror an ActiveRecord object myself).

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the mock_model/stub_model behavior that mirrors ActiveRecord and compare its existing new_record? query method. Determine the intended default and scope for a readonly? response, including whether per-test configuration is required. Done means the mock exposes the expected readonly? behavior and the issue's ActiveRecord example is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
backend, testing-qa
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.