Creating a Hello World Test Project using Fixie

A couple months ago as part of the interview process I was directed to a website called HackerRank, for a coding challenge . HackerRank has a beginners challenge called "30 Days of programming". I am using the Day 0 Hello World application here as the code to be tested.

Given the interview process, seems to involve a ton of Console app challenges, I thought I would use this as another quick start Default Convention example of Fixie

The code:

This is the Day0 code we are going to test:

namespace Day0  
{
  using System;

  class Program
  {
    static void Main(string[] args)
    {
      // Declare a variable named 'inputString' to hold our input.
      String inputString;

      // Read a full line of input from stdin (cin) and save it to our variable, input_string.
      inputString = Console.ReadLine();

      // Print a string literal saying "Hello, World." to stdout using cout.
      Console.WriteLine("Hello, World.");

      // TODO: Write a line of code here that prints the contents of input_string to stdout.
      Console.WriteLine(inputString);
    }
  }
}

Step-by-step:

Create new Class Library named Day0Tests
File->New->Project

Add Nuget packages from Nuget Package Manager Console.
Tools->Nuget Package Manager->Nuget Package Manager Console

PM> Install-Package Fixie  
...
PM> Install-Package Shouldly  
...

Rename the default class.cs file created in the template to Day0Tests.cd and implement code below:

namespace Day0Tests  
{
  using Day0;
  using Shouldly;
  using System;
  using System.IO;

  public class Day0Tests
  {

    public StringWriter StringWriter { get; } = new StringWriter();

    private void CaptureConsole(string aInput)
    {
      var stringReader = new StringReader(aInput);
      Console.SetIn(stringReader);
      Console.SetOut(StringWriter);
    }

    public void Case1()
    {
      //Arrange
      string input = "Welcome to 30 Days of Code!";
      string expectedOutput = "Hello, World.\r\nWelcome to 30 Days of Code!\r\n";
      CaptureConsole(input);

      //Act
      Program.Main(null);

      //Gather Output
      StringWriter.Flush();
      string output = StringWriter.ToString();

      //Assert
      output.ShouldBe(expectedOutput);
    }

    public void Case2()
    {
      //Arrange
      string input = "HackerRank is the best!";
      string expectedOutput = "Hello, World.\r\nHackerRank is the best!\r\n";
      CaptureConsole(input);

      //Act
      Program.Main(null);

      //Gather Output
      StringWriter.Flush();
      string output = StringWriter.ToString();

      //Assert
      output.ShouldBe(expectedOutput);
    }
  }
}

The Code is rather simple and follows the Arrange Act Assert concept. All the conventions are the Fixie Default.

More fixie blogs to come as I update the JimmyMVC to demonstrate integration tests.

CaptureConsole

A nice method is CaptureConsole to pass input (stdin) and capture output (stdout) from a console application.