freerange / mocha

A mocking and stubbing library for Ruby
https://mocha.jamesmead.org
Other
1.23k stars 158 forks source link

Fix regression when stubbed method expects Hash but receives ActionController::Parameters object #664

Closed floehopper closed 3 months ago

floehopper commented 3 months ago

This was broken in this previous commit which was part of https://github.com/freerange/mocha/pull/660 and which was released in v2.4.3.

Previously the only requirement we put on Hash-like arguments was that they respond to #keys & #[] methods. However, the change in that commit we introduced an implicit assumption that they also respond to a #length method which is caused the problem described here.

However, ActionController::Parameters is commonly used in Rails apps and it does not respond to #length. So it seems better to add a guard condition to ensure the object responds to #keys and use that to determine the length.

Note that the HasEntry, HasKey and HasKeys matchers already work this way and so this feels like it makes HasEntries more consistent.

floehopper commented 3 months ago

Released in v2.4.5.