Read more

Stubbing terminal user input in RSpec

Arne Hartherz
July 05, 2016Software engineer at makandra GmbH

Reading user input in console applications is usually done using Kernel#gets. Stubbing that can be a bit hairy.

Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

When your code expects user input, you can not say Kernel.stub(gets: 'user input'). This will have no effect because of reasons.

Instead, you need to know which class will call gets. For example:

described_class.any_instance.stub(gets: 'user input')

If you do not know where gets is called, you can try something like this:

Object.any_instance.stub(gets: 'user input')

Any instance of an object should eventually hit that stub.

Posted by Arne Hartherz to makandra dev (2016-07-05 11:11)