Q:

How to test a thread that reads from Console.ReadLine i.e. how do I write to the console's input?

I've got a thread that reads from the console via Console.ReadLine, but to build a unit test for that thread, I want to essentially write to the console's input, I tried this:
Stream inputStream = Console.OpenStandardInput();
StreamWriter sw = new StreamWriter(inputStream);
sw.WriteLine("foo");

But the thread doesn't see the text? Is there another way?

A:

I think you need to take a step back on this one. What you have done is couple your application to the console. Whereas you should really separate it... Here is some pseudo-code:
Your Application
public interface IUserInput
{
    string ReadInput();
}

public class ConsoleInput : IUserInput
{
    public ReadInput()
    {
        return Console.ReadLine();
    }
}

public class YourClass
{
    IUserInput _userInput;

    // Can inject TEST or REAL input
    public YourClass(IUserInput userInput)
    {
        _userInput = userInput;
    }

    // ... Your code
    public void YourMethod()
    {
        var doSomething = _userInput.ReadInput();
    }
}

Your Test
public class TestInput : IUserInput
{
    public ReadInput()
    {
        return "This is dummy data";
    }
}

[Test]
public void MyTest()
{
    var testInput = new TestInput();
    var systemUnderTest = new YourClass(testInput);
    // ...
}

