How to Test::Unit a module that prints?

Some of the things I am writing print to STDOUT. How do you put that into a
test case for Test::Unit?

Thanks!

Leam

How about not printing to the constant STDOUT, but to some destination
that is passed in to the creation of the object or the initialization
of the system? In the tests, this can be a mock, to verify that the
right methods were called on it. Or it can be a destination you can
later inspect to see what its "contents" are.

-Dave

···

On Mon, Jul 25, 2016 at 7:54 AM, leam hall <leamhall@gmail.com> wrote:

Some of the things I am writing print to STDOUT. How do you put that into a
test case for Test::Unit?

--
Dave Aronson, consulting software developer of Codosaur.us,
PullRequestRoulette.com, Blog.Codosaur.us, and Dare2XL.com.

One possible trick there is to print to $stdout instead.

STDOUT is a constant that has the original standard output of the process,
but in Ruby you are expected to use the variable $stdout instead. For
example, puts prints to $stdout, not to STDOUT.

Using $stdout allows programs to switch to something else while preserving
the original stream in the constant. For example, in your case you could do
something like (untested):

    require 'stringio'

    def assert_stdout(expected)
      original_stdout = $stdout
      $stdout = StringIO.new
      yield
      assert_equal expected, $stdout.string
    ensure
      $stdout = original_stdout
    end

···

On Mon, Jul 25, 2016 at 1:54 PM, leam hall <leamhall@gmail.com> wrote:

Some of the things I am writing print to STDOUT. How do you put that into
a test case for Test::Unit?

Hey all, sorry for the confusion. Here's a more detailed explanation.

One of the code items I'm trying to test is PresenterDefault, which is given a Character object by Presenter to print out in a standard format. I don't know how to capture the results of the print statement to test.

https://github.com/LeamHall/CT_Character_Generator/blob/refactor_character/lib/PresenterDefault.rb

It will print out a line of data, like:

  Marco Domici 79AA8B 18 Noble: 1 Marine: 2 VaccSuit-1 GunCbt-1

The data is pulled from a Character object and can be set, so the exact line can be tested. The issue is that the program is made to print to the screen, or whatever is being used as standard out.

There are other formats planned, CSV, Wiki, and whatever else I can find. I'm trying to improve my Ruby (1.8.7) skills; specifically in OOP and Testing. If there's an architecture issue that's causing this, feel free to point it out.

···

On 07/25/16 08:20, Dave Aronson wrote:

On Mon, Jul 25, 2016 at 7:54 AM, leam hall <leamhall@gmail.com> wrote:

Some of the things I am writing print to STDOUT. How do you put that into a
test case for Test::Unit?

How about not printing to the constant STDOUT, but to some destination
that is passed in to the creation of the object or the initialization
of the system? In the tests, this can be a mock, to verify that the
right methods were called on it. Or it can be a destination you can
later inspect to see what its "contents" are.

-Dave

capture_io, assert_silent, and assert_output already exist in minitest.

···

On Jul 25, 2016, at 05:04, Xavier Noria <fxn@hashref.com> wrote:

    require 'stringio'

    def assert_stdout(expected)
      original_stdout = $stdout
      $stdout = StringIO.new
      yield
      assert_equal expected, $stdout.string
    ensure
      $stdout = original_stdout
    end

One of the code items I'm trying to test is PresenterDefault, which is
given a Character object by Presenter to print out in a standard format. I
don't know how to capture the results of the print statement to test.

https://github.com/LeamHall/CT_Character_Generator/blob/refactor_character/lib/PresenterDefault.rb

It will print out a line of data, like:

        Marco Domici 79AA8B 18 Noble: 1 Marine: 2 VaccSuit-1 GunCbt-1

The data is pulled from a Character object and can be set, so the exact
line can be tested. The issue is that the program is made to print to the
screen, or whatever is being used as standard out.

You use something like the assert_stdout custom assertion I sent in the
first reply, and pass to it what the test expects.

BTW, Ruby 1.8.7 is really old, it's going to be easy to find code examples
or gems that don't work on it. If you can, better use a modern version.

Can we talk about this code ?

···

On Mon, Aug 1, 2016 at 6:34 PM, Ryan Davis <ryand-ruby@zenspider.com> wrote:

> On Jul 25, 2016, at 05:04, Xavier Noria <fxn@hashref.com> wrote:
>
> require 'stringio'
>
> def assert_stdout(expected)
> original_stdout = $stdout
> $stdout = StringIO.new
> yield
> assert_equal expected, $stdout.string
> ensure
> $stdout = original_stdout
> end

capture_io, assert_silent, and assert_output already exist in minitest.

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

Yes, last I heard people were using 2 something and minitest, which I
found to be a great improvement, and in some ways better than RSpec.

···

On Tue, Jul 26, 2016 at 4:26 PM, Xavier Noria <fxn@hashref.com> wrote:

One of the code items I'm trying to test is PresenterDefault, which is
given a Character object by Presenter to print out in a standard format. I
don't know how to capture the results of the print statement to test.

https://github.com/LeamHall/CT_Character_Generator/blob/refactor_character/lib/PresenterDefault.rb

It will print out a line of data, like:

        Marco Domici 79AA8B 18 Noble: 1 Marine: 2 VaccSuit-1 GunCbt-1

The data is pulled from a Character object and can be set, so the exact
line can be tested. The issue is that the program is made to print to the
screen, or whatever is being used as standard out.

You use something like the assert_stdout custom assertion I sent in the
first reply, and pass to it what the test expects.

BTW, Ruby 1.8.7 is really old, it's going to be easy to find code examples
or gems that don't work on it. If you can, better use a modern version.

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

Don't ask to ask, just ask.

···

On Aug 1, 2016, at 19:13, Jeff Rain <jeffrain21@gmail.com> wrote:

Can we talk about this code ?

Why did you write it ? I wanna improve my dev skills

···

On Tue, Aug 2, 2016 at 2:50 AM, Ryan Davis <ryand-ruby@zenspider.com> wrote:

> On Aug 1, 2016, at 19:13, Jeff Rain <jeffrain21@gmail.com> wrote:
>
> Can we talk about this code ?

Don't ask to ask, just ask.

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

I wrote it because it was useful to stuff I was doing at the time and seemed like others would benefit from it. I also wrote it because I like to write code.

If you want to improve your dev skills then write more code and read more code. Think about why you're writing the code the way you're writing it. Think about how it might be better if it were written differently. Think about why your approach might be deficient or superior. Think about some code you just read and where the author was mentally at the time they wrote it. What did they do right? What can you learn from it? What did they do wrong? What can you learn from that? Etc...

You get better by practicing.

···

On Aug 2, 2016, at 10:07, Jeff Rain <jeffrain21@gmail.com> wrote:

Why did you write it ? I wanna improve my dev skills