我有以下方法。如何为此功能编写单元测试以确保将机器添加到维修列表中?
public void AddMachineToRepairsList()
{
Console.WriteLine("Would you like to add this Machine to the repairs list?");
var addToRepairs = Console.ReadLine().ToLower();
if (addToRepairs == "yes")
{
int cost = 0;
int hoursWorked = 0;
var machine = new Repair(cost, hoursWorked);
Repairs.Add(machine);
Console.WriteLine("Machine successfully added!");
}
else
{
Console.WriteLine("Please enter machine information again");
this.Run();
}
}
答案 0 :(得分:0)
您需要将addToRepairs作为参数传递给方法,并调用
Console.WriteLine("Would you like to add this Machine to the repairs list?");
var addToRepairs = Console.ReadLine().ToLower();
while(AddMachineToRepairsList(addToRepairs)==false)
{
Console.WriteLine("Please enter machine information again");
this.Run();
}
定义将是
public bool AddMachineToRepairsList(string option)
{
string addToRepairs = "";
if (addToRepairs == "yes")
{
int cost = 0;
int hoursWorked = 0;
var machine = new Repair(cost, hoursWorked);
Repairs.Add(machine);
Console.WriteLine("Machine successfully added!");
return true;
}
else
{
return false;
}
}
现在,一个单元测试将具有价值"是"和#34;不" 。您 可以编写单元测试并根据传递的选项断言返回值为true / false