如何在代码中测试此方法? 我是否只需要在输入或其他内容中输入一个值? 应该通过正确创建带有正确细节的歌曲来检查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);
}
答案 0 :(得分:3)
答案 1 :(得分:0)
有两种方法可以测试输入和或方法。
在每次输入后打印结果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/en-us/visualstudio/test/unit-test-basics?view=vs-2017
答案 2 :(得分:0)
为您提供单元测试的示例。该代码使用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);
}
}