DEV Community

Masa
Masa

Posted on

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

rspec-nlet

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.

Usage

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) 

Note

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

https://github.com/rspec/rspec-core/issues/1650

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!

references

Top comments (0)