简体   繁体   中英

How would I unit-test this c# method that uses the Console?

How would I test this method within the code? Do I just need to put a value into the inputs I have or something else? It should test ISD is working by checking by properly creating a song with the correct details

static Song InputSongDetails()
{
    Console.WriteLine("What is the name of your song");
    string name = Console.ReadLine();

    Console.WriteLine("What is the artists name");
    string artist = Console.ReadLine();

    int records;
    Console.WriteLine("How many records did it sell");
    while (!int.TryParse(Console.ReadLine(), out records) || records < 0)
    {
        Console.WriteLine("That is not valid please enter a number");
    }
    return new Song(name, artist, records);
}

The best approach would probably be to abstract away the Console using some interface. But, you can also pre-fill the In buffer of the Console with your desired data.

For example:

var data = String.Join(Environment.NewLine, new[]
{
    "Let it be",
    "Beatles",
    // ...
});

Console.SetIn(new System.IO.StringReader(data));

// usage:
var songName = Console.ReadLine();
var artistName = Console.ReadLine();

See MSDN

There are two ways you can test the input and or the method.

after each input print the result Console.WriteLine(MyVar)

static Song InputSongDetails()
{
    Console.WriteLine("What is the name of your song");
    string name = Console.ReadLine();
    Console.WriteLine(name)

    Console.WriteLine("What is the artists name");
    string artist = Console.ReadLine();
    Console.WriteLine(artist)

    int records;
    Console.WriteLine("How many records did it sell");
    while (!int.TryParse(Console.ReadLine(), out records) || records < 0)
    {
        Console.WriteLine("That is not valid please enter a number");
    }
    Console.WriteLine(records)
    return new Song(name, artist, records);
}

You can also separate the method to input param you already validated.

static Song InputSongDetails(string name,string artist, int records)
    {
        return new Song(name, artist, records);
    }

and then you can just create a simple unit test for the method

Please read https://docs.microsoft.com/en-us/visualstudio/test/unit-test-basics?view=vs-2017

An example of a unit test for you. The code uses NSubstitute for mock object.

public class Song
{
    public string Name { get; }

    public string Artist { get; }

    public int Records { get; }

    public Song(string name, string artist, int records)
    {
        Name = name;
        Artist = artist;
        Records = records;
    }
}

public interface IInOutService
{
    void Ask(string question);

    string GetString();

    string AskValue(string question);
}

public class InOutService : IInOutService
{
    public void Ask(string question)
    {
        Console.WriteLine(question);
    }

    public string GetString()
    {
        return Console.ReadLine();
    }

    public string AskValue(string question)
    {
        Ask(question);
        return GetString();
    }
}

public class FactorySong
{
    private readonly IInOutService _inOutService;

    public FactorySong(IInOutService inOutService)
    {
        _inOutService = inOutService;
    }

    public Song Create()
    {
        var name = _inOutService.AskValue("What is the name of your song");
        var artist = _inOutService.AskValue("What is the artists name");

        int records;
        _inOutService.Ask("How many records did it sell");

        while (!int.TryParse(_inOutService.GetString(), out records) || records < 0)
        {
            _inOutService.Ask("That is not valid please enter a number");
        }

        return new Song(name, artist, records);
    }
}

[TestClass]
public class FactorySongTest
{
    [TestMethod]
    public void TestCreate()
    {
        var consoleService = Substitute.For<IInOutService>();
        var testString = "test";
        var testRecords = 1;

        consoleService.AskValue(Arg.Any<string>()).Returns(testString);
        consoleService.GetString().Returns(testRecords.ToString());

        var factory = new FactorySong(consoleService);
        var song = factory.Create();

        Assert.IsNotNull(song);
        Assert.IsTrue(testString == song.Name);
        Assert.IsTrue(testString == song.Name);
        Assert.AreEqual(testRecords, song.Records);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM