Today I Learned

hashrocket A Hashrocket project

Regex in RSpec Argument Matchers

Today I learned you can use regular expressions in RSpec method argument expectations.

Suppose I have a method that takes an email, and a registered boolean as parameters:

def some_method(email:, registered:)

In a spec, it's easy enough to verify that it was called with set parameters, like and true:

  .to receive(:some_method)
  .with(email: '', registered: true)

But what if I want to verify that the email address just belongs to We can use a regex for that!

  .to receive(:some_method)
  .with(email: /$/, registered: true)
See More #ruby TILs
Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.