Testing gets.chomp using minitest

Hi everyone,

I'm using gets.chomp in my code to prompt user for input.

def get_action
  gets.chomp
end

How would you stub this using minitest?

Thanks

You could just stub the `gets` method, like:

   def object_under_test.gets
     "foo\n"
   end

then do a check like

   assert_equal(get_action, "foo")

Like usually when stubbing methods, this defines your implementation though.
While your original code will in many cases work the same when changed to $stdin.gets.chomp for example, the test would fail in that case.

Another approach would be to fork a process where you run the method under test, then feed it input on stdin via your test process.

hope this helps,

- recmm

···

On 06/29/2016 02:04 PM, angela ebirim wrote:

def get_action
   gets.chomp
end

How would you stub this using minitest?

Thanks Recursive!

···

On 29 June 2016 at 15:16, Recursive Madman <recursive.madman@gmx.de> wrote:

On 06/29/2016 02:04 PM, angela ebirim wrote:

def get_action
   gets.chomp
end

How would you stub this using minitest?

You could just stub the `gets` method, like:

  def object_under_test.gets
    "foo\n"
  end

then do a check like

  assert_equal(get_action, "foo")

Like usually when stubbing methods, this defines your implementation
though.
While your original code will in many cases work the same when changed to
$stdin.gets.chomp for example, the test would fail in that case.

Another approach would be to fork a process where you run the method under
test, then feed it input on stdin via your test process.

hope this helps,

- recmm

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;