Skip to content

Failing test for options hash / kwarg compat - #20

Closed
tjefferson08 wants to merge 1 commit into
testdouble:mainfrom
tjefferson08:opts-hash-in-initializer
Closed

tjefferson08 wants to merge 1 commit into
testdouble:mainfrom
tjefferson08:opts-hash-in-initializer

Conversation

@tjefferson08

@tjefferson08 tjefferson08 commented Nov 21, 2022 •

Copy link
Copy Markdown

Only this time, it's in the initializer!

Very much appreciate the work done in #17 and #14

If I understand the landscape correctly, I'm asking about extending similar compatibility/support to initializers

# Running:

................................................................................E.........................................

Finished in 0.230618s, 529.0133 runs/s, 1920.9255 assertions/s.

  1) Error:
OfNextTest#test_options_hash_initializer:
ArgumentError: unknown keyword: :item [Mocktail call: `(item: "hello")']
    /Users/travis/mocktail/test/safe/of_next_test.rb:64:in `test_options_hash_initializer'

Only this time, it's in the initializer!
@searls

searls commented Nov 21, 2022

Copy link
Copy Markdown
Member

Give a mouse a method… 🐭

rosston added a commit that referenced this pull request Oct 12, 2026
Replacing a class swaps `new` and its class methods for generic
`(*args, **kwargs)` lambdas, so Ruby always splits brace-less keywords
like `Klass.new(item: "hello")` into kwargs. Mocktail then validated
those kwargs against the real method, and when that method takes an
options hash (`def initialize(opts = {})`) rather than keywords, it
raised `ArgumentError: unknown keyword: :item`, even though Ruby 3
passes keywords to a method with no keyword params as a trailing
positional hash. Instance-method doubles never had this problem
because their lambdas are generated from the real signature.

Add FoldsKeywordsIntoOptionsHash, which applies Ruby's rule: fold
non-empty kwargs into a trailing hash when the original method has no
:key, :keyreq, :keyrest, or :nokey params. It reads params through
GrabsOriginalMethodParameters so Sorbet-wrapped methods report their
real signature. Call it before building the Call in HandlesDryNewCall
and in the mocked branch of replaced class methods, so validation,
call logging, and stub matching all see the same shape instance
doubles already record.

test_replace_class asserted that `House.room(wrong: :arg)` raises for
`def self.room(room_name)`, which real Ruby accepts, so it now asserts
on a call Ruby also rejects.

Picks up the failing test from #20 and makes it pass.
@rosston

rosston commented Oct 12, 2026

Copy link
Copy Markdown
Member

This was fixed in #34 and released in 3.0.1.

@rosston rosston closed this Oct 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants