DEV Community


Posted on

rspec-nlet: RSpec extension to define multiple helpers with let


One day I was testing some command line tool I built with Ruby and I tried to write a test code like this.

let(:msg, :status) { Open3.capture2e(command) }

But I realized RSpec didn't allow you to define multiple helpers with let at once.

So I created a gem for it! rspec-nlet
I'm aware that the use cases of this gem is very rare haha.


You can define a single helper or multiples helpers with it at once.
You get errors if the names and returned values from the block don't match.

You can read README for details.

# define multiple helpers
> let(:one, :two, :three) { [1,2,3] }
> one # => 1
> two # => 2
> three # => 3

# define a helper
> let(:single) { 'single' }
> single # => 'single'

# raise an error when too few names are passed
> let(:too, :few) { [1,2,3] }
> few # => StandardError (Too few names)

# raise an error when too many names are passed
> let(:too, :many) { [1] }
> many # => StandardError (Too many names)


This feature was actually proposed to rspec-core in the past.

But it was rejected for a couple of reasons:

  • lets is not a good name
  • not so many use cases
  • handling corner cases

But it can live outside as a gem!

Thank you for reading!


Top comments (0)