Try reordering seemingly symmetric arguments if type errors occur

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the dumb-mistakes-and-gotchas category.

Last Updated: 2024-04-24

This code in one of my tests failed

assert_match(flash[:error], /invalid/)

The error it threw was:

TypeError: no implicit conversion of Regexp into String

  test/integration/user_sessions_test.rb:35:in `block in <class:UserSessionController>'

I tried a bunch of type conversions on my regex /invalid/, before eventually realizing that the API was simply strict about which argument number the regex was given in (whereas other test frameworks are flexible about this)

The fix was to switch the argument order:

assert_match(/invalid/, flash[:error])

Lesson

If a function with seeminly symmetric arguments is throwing type errors, try switching the argument order.