DEV Community

Masa
Masa

Posted on

3

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

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay