Testing STDIN in Ruby -


I am currently trying to test a basic method that receives some input from the user (ho) and It (puts) it. After some research I found a good way to test the standard output stream, which is given below:

  def capture_standard_output (and block) original_stream = $ stdout $ stdout = duplicate = string IO.new Produce mock.string.chomp Make sure $ stdout = original_stream end is  

the method I am testing below is one, the output and input refers to the Ivars that I started in the beginning Start and point out $ stdout & amp; $ Stdin:

  def ask_for_mark ouput.puts 'What will I say to you today?' Answer = input.gets.chomp.capitalize answer end  

Now I have seen some solutions for STDIN but in reality nobody understood them and I definitely do not want to copy I am Paste it. The only one to do "work" is given below, but it is not really working when I run RSPC and it waits for input and just logs by pressing, it passes:

  "takes the name of the user and returns it" output = caption_stands_output {game.ask_for_name} is expected (output). Eq "What will I call you today?" Game.input.stub (: gets) {'which'} is expected (game.ask_for_name). What will be a good way of investigating STDIN for AK 'Joe End End? I am staring at the screen for most days (no good, I know) so a fresh approach and some help would be greatly appreciated: -)  

update

for the same Anyone who faces the issues, I followed a different (simple) approach. First of all, I will separate the IO operation from my own way.

In the case of input, it can pre-populate with the desired data in the tests, when the message is done , the message will be returned to it. What looks different looks like the new line \ n :

  input = StringIO.new ("one \ nto two \ n") => # & Lt; StringIO: 0x007f88152f3510 & gt; Input.gets = & gt; "One \ n" input.gets => "Two \ n" input.gets => Zero  

This helps in keeping the work internally under the test, without adding to the tests for the details of the implementation, private.

Another way is to use only polymorphism and pass in a fake or a spy object that corresponds to the same API, but stdin or stdout Instead of calling , or in return for packaged data back or in the case of a spy, it registers the call.

  Is the class SPOOF def introduced? = False end ... def ask_for_name // call at stdout will generally take the wrong place? = True end ... end class FakeIO def start (data = [some, data]) @ data = data end def get_user_input // call STDIN will normally end @ data.shift ... end   

There is a business exchange in every approach but I thought that I keep them here if any one is thinking about such issues or options.

You just stub STDIN :

  It "takes the name of the user and gives it back" output = Captain_Stand_output {game.ask_for_name} is expected (production). Eq "What will I call you today?" (STDIN). (): ('Gets') {'which'} is expected (game.ask_for_name). In fact, you can use the  STDOUT ,  $ stdout : 

  Without the need to change it, it "seeks the name of the user and returns" (STDOT). To obtain: (): ("What will happen?") (SITDIN) to get permission (STDIN) ('Gets') ('which') is expected (game.ask_for_name). AE Joe End End  

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

lua - HowTo create a fuel bar -