繁体   English   中英

如何对使用控制台的c#方法进行单元测试?

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

我将如何在代码中测试此方法? 我是否只需要在输入或其他内容中输入一个值? 它应通过正确创建带有正确细节的歌曲进行检查,从而测试ISD是否正常工作

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);
}

最好的方法可能是使用某些界面将Console抽象化。 但是,您也可以用所需的数据预填充ConsoleIn缓冲区。

例如:

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();

参见MSDN

您可以通过两种方式测试输入和/或方法。

在每个输入之后打印结果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);
}

您也可以将方法分开输入已验证的参数。

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

然后您可以为该方法创建一个简单的单元测试

请阅读https://docs.microsoft.com/zh-cn/visualstudio/test/unit-test-basics?view=vs-2017

为您提供单元测试的示例。 该代码使用NSubstitute作为模拟对象。

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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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