Stubbing terminal user input in RSpec

Posted . Visible to the public.

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

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.

Arne Hartherz
Last edit
Arne Hartherz
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2016-07-05 09:11)