Testing Output With RSpec
Tags: tdd, testing, rspec, stringio
Published: 2020-09-01 11:49:15

---
title: Testing Output With RSpec
published: true
date: 2019-04-09 00:00:00 UTC
tags: 
canonical_url: https://ellehallal.dev/blog/2019/04/2019-04-09-testing-output-with-rspec/
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/1fiqd8j76qnmpkeqv1z8.png
tags: #tdd, #testing, #rspec, #stringIO
---
<center><h3>Originally posted at <a href="https://ellehallal.dev" target="_blank">ellehallal.dev</a>👩🏽‍💻</h3></center>

---

This is a quick blog on testing output with RSpec. Currently, I am working on a [Tic Tac Toe game in Ruby](https://github.com/ellehallal/ruby-tic-tac-toe), where a user can play against a computer.

---

## Previous test attempts

I’ve experienced issues testing a loop in the game, where it keeps requesting moves from players until the board is full or a player has won.

```java
def main_game
  play_move until @game.over?
  end_of_game
end
```

One attempt at testing this was to spy on the `play_move` and `end_of_game` methods. This worked fine if a full board was provided.

However, this was proving difficult to test when an incomplete board was present. Despite simulating user input, the test would hang at this stage. The same issue occurred when using [object doubles](https://relishapp.com/rspec/rspec-mocks/v/3-8/docs/verifying-doubles/using-an-object-double).

---

## Testing output

One of my mentors suggested testing the output of the `end_of_game` method. The `end_of_game` method prints the Tic Tac Toe board and the outcome of the game.

```java
it 'plays a game that ends with a winning player' do

  expect { controller.main_game }
  .to output("""

  1 | x | x
-----------
  o | o | x
-----------
  x | o | o
The current player is x
Choose a position from 1-9:

  x | x | x
-----------
  o | o | x
-----------
  x | o | o
x is the winner!""")
  .to_stdout
```

The multi-lines and alignment was also proving difficult to get right for the expected output. The key element that needed to be tested was the outcome of the game, which was the last line of the output: 'x is the winner!'

---

## StringIO

StringIO is a ‘[string-based replacement for an IO object. It acts same as a file, but it’s kept in memory as a String](https://stackoverflow.com/questions/37526074/what-is-stringio-in-the-context-of-rspec-testing-ruby-on-rails)’.

The idea is to catch the stream of output and redirect it. To check the last line of the output was correct, my mentor suggested trying the following:

```java
it 'plays a game that ends with a winning player' do
  controller = controller_setup([1, 'x', 'x', 4, 'o', 'x', 'x', 8,     'o'])
  allow($stdin).to receive(:gets).and_return('8', '4', '1')
  $stdout = *StringIO*.new

  controller.main_game
  output = $stdout.string.split("\n")

  expect(output.last).to eq('x is the winner!')
end
```

In this test:

- A new instance of the `controller` class is created, with an incomplete board

- The input is simulated to play the three remaining positions on the board

- The standard output `$stdin` is redirected to a new instance of `StringIO`

- The `main_game` method is called to play and complete the game

- The output is then saved to the variable `output`, converted to a string, and split at the points where there is a new line to create an array of strings

- The last item in the array is expected to be 'x is the winner!'

I’ll continue to use this method when testing large blocks of text with multiple lines. It’s significantly simpler than trying to align multi-line outputs in a test.

* * *

## Resources

- [IO in Ruby](https://thoughtbot.com/blog/io-in-ruby#stringio)
- [Class: IO (Ruby 2.3.1)](https://ruby-doc.org/core-2.3.1/IO.html)
- [How can I capture STDOUT to a string?](https://stackoverflow.com/questions/14987362/how-can-i-capture-stdout-to-a-string/22777806)