使用Nunit在单元测试期间实现接口列表
我目前正在学习C#,我对一项简单的任务感到震惊。 我有此代码要测试:
public interface IAppointment
{
public string PatientName { get; set; }
public IEnumerable<DateTime> ProposedTimes { get; set; }
public DateTime? SelectedAppointmentTime { set; }
}
public static class MedicalScheduler
{
public static Dictionary<DateTime, string> Appointments { get; set; } = new Dictionary<DateTime, string>();
public static List<DateTime> FreeSlots { get; set; } = new List<DateTime>();
public static IEnumerable<Tuple<string, bool>> Schedule(IEnumerable<IAppointment> requests)
{
bool slotFound = false;
foreach (var appointment in requests)
{
if (slotFound) continue;
foreach (var times in appointment.ProposedTimes)
{
var freeSlot = FreeSlots.Where(s => s.Date == times.Date).FirstOrDefault();
if (freeSlot != null)
{
slotFound = true;
Appointments.Remove(freeSlot);
appointment.SelectedAppointmentTime = freeSlot;
yield return new Tuple<string, bool>(appointment.PatientName, true);
}
}
yield return new Tuple<string, bool>(appointment.PatientName, false);
}
}
}
而且我需要用一组参数测试“时间表”。例如,我需要用空的约会和自由职业者进行测试,但在“请求”中有一个元素。 我想我已经了解了如何编译单元测试并设置字典和列表参数。但是我不确定如何创建IEnumerable变量。 我的想法是创建IAPPOINT的列表,但是如何在测试单元中实现接口?我已经尝试使用欧格,但我不明白如何正确使用它。
很抱歉,如果请求似乎很困惑,但是我不知道如何更好地解释:)
提前感谢您的帮助。
I'm currently studying C# and I'm quiet stunned over a simple task.
I have this code to test:
public interface IAppointment
{
public string PatientName { get; set; }
public IEnumerable<DateTime> ProposedTimes { get; set; }
public DateTime? SelectedAppointmentTime { set; }
}
public static class MedicalScheduler
{
public static Dictionary<DateTime, string> Appointments { get; set; } = new Dictionary<DateTime, string>();
public static List<DateTime> FreeSlots { get; set; } = new List<DateTime>();
public static IEnumerable<Tuple<string, bool>> Schedule(IEnumerable<IAppointment> requests)
{
bool slotFound = false;
foreach (var appointment in requests)
{
if (slotFound) continue;
foreach (var times in appointment.ProposedTimes)
{
var freeSlot = FreeSlots.Where(s => s.Date == times.Date).FirstOrDefault();
if (freeSlot != null)
{
slotFound = true;
Appointments.Remove(freeSlot);
appointment.SelectedAppointmentTime = freeSlot;
yield return new Tuple<string, bool>(appointment.PatientName, true);
}
}
yield return new Tuple<string, bool>(appointment.PatientName, false);
}
}
}
And I'm required to test "Schedule" with a certain set of parameters. For example, I need to test it with empty Appointments and FreeList but with a single element in "requests".
I think I have understood how to compile a Unit Test and to set the Dictionary and List parameters. But I'm not sure how to create the IEnumerable variable.
My idea was to create a List of IAppointment(s), but how can I implement the interface in the test unit? I have tried using Moq but I didn't understood how I should use it correctly.
I'm sorry if the request seems quite confusing, but I don't know how to explain better :)
Thanks in advance for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参见以下示例:
MedicalScheduler.schedule
接受实现任何参数iEnumerable&lt; iAppointment&gt;
,例如iappointment&gt; 。因此,您只需创建
list&lt; iAppointment&gt;
,然后用iappointment
的自定义实例填充它。您可以像我在示例中一样使用MOQ来创建实例。但是对于我自己的项目,我更喜欢建造者模式:
Please see the following example:
MedicalScheduler.Schedule
accepts any parameter implementingIEnumerable<IAppointment>
, e. g.List<IAppointment>
orCollection<IAppointment>
.So you simply create a
List<IAppointment>
and fill it with custom instances ofIAppointment
.You can use Moq for creating the instances, as I did in the example. But for my own projects, I prefer the builder pattern: