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 web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
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)