使用Nunit在单元测试期间实现接口列表

发布于 2025-01-31 23:45:55 字数 1616 浏览 2 评论 0原文

我目前正在学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的故事 2025-02-07 23:45:55

请参见以下示例:

[Test]
public void Schedule()
{
    // Arrange
    var appointmentMock = new Mock<IAppointment>();
    appointmentMock.Setup(appointment => appointment.PatientName).Returns("Dixie Dörner");
    appointmentMock.Setup(appointment => appointment.ProposedTimes).Returns(
        new List<DateTime>
        {
            new DateTime(1953,4,12), 
            new DateTime(1953,4,13)
        });
    var requests = new List<IAppointment>{appointmentMock.Object};

    // Act
    var results = MedicalScheduler.Schedule(requests);

    // Assert
    Assert.IsTrue(results.Any());
    // results.Should().HaveCount(1); // If you're using FluentAssertions
}

MedicalScheduler.schedule接受实现任何参数iEnumerable&lt; iAppointment&gt;,例如iappointment&gt; 。

因此,您只需创建list&lt; iAppointment&gt;,然后用iappointment的自定义实例填充它。

您可以像我在示例中一样使用MOQ来创建实例。但是对于我自己的项目,我更喜欢建造者模式:

internal static class AppointmentBuilder
{
    public static IAppointment CreateDefault() => new Appointment();

    public static IAppointment WithPatientName(this IAppointment appointment, string patientName)
    {
        appointment.PatientName = patientName;
        return appointment;
    }
    
    public static IAppointment WithProposedTimes(this IAppointment appointment, params DateTime[] proposedTimes)
    {
        appointment.ProposedTimes = proposedTimes;
        return appointment;
    }
    
    private class Appointment : IAppointment
    {
        public string PatientName { get; set; }
        public IEnumerable<DateTime> ProposedTimes { get; set; }
        public DateTime? SelectedAppointmentTime { get; set; }
    }
}

[Test]
public void Schedule()
{
    // Arrange
    var requests = new List<IAppointment>{AppointmentBuilder.CreateDefault()
        .WithPatientName("Dixie")
        .WithProposedTimes(new DateTime(1953,4,12))};

    // Act
    var results = MedicalScheduler.Schedule(requests);

    // Assert
    Assert.IsTrue(results.Any());
    // results.Should().HaveCount(1); // If you're using FluentAssertions
}

Please see the following example:

[Test]
public void Schedule()
{
    // Arrange
    var appointmentMock = new Mock<IAppointment>();
    appointmentMock.Setup(appointment => appointment.PatientName).Returns("Dixie Dörner");
    appointmentMock.Setup(appointment => appointment.ProposedTimes).Returns(
        new List<DateTime>
        {
            new DateTime(1953,4,12), 
            new DateTime(1953,4,13)
        });
    var requests = new List<IAppointment>{appointmentMock.Object};

    // Act
    var results = MedicalScheduler.Schedule(requests);

    // Assert
    Assert.IsTrue(results.Any());
    // results.Should().HaveCount(1); // If you're using FluentAssertions
}

MedicalScheduler.Schedule accepts any parameter implementing IEnumerable<IAppointment>, e. g. List<IAppointment> or Collection<IAppointment>.

So you simply create a List<IAppointment> and fill it with custom instances of IAppointment.

You can use Moq for creating the instances, as I did in the example. But for my own projects, I prefer the builder pattern:

internal static class AppointmentBuilder
{
    public static IAppointment CreateDefault() => new Appointment();

    public static IAppointment WithPatientName(this IAppointment appointment, string patientName)
    {
        appointment.PatientName = patientName;
        return appointment;
    }
    
    public static IAppointment WithProposedTimes(this IAppointment appointment, params DateTime[] proposedTimes)
    {
        appointment.ProposedTimes = proposedTimes;
        return appointment;
    }
    
    private class Appointment : IAppointment
    {
        public string PatientName { get; set; }
        public IEnumerable<DateTime> ProposedTimes { get; set; }
        public DateTime? SelectedAppointmentTime { get; set; }
    }
}

[Test]
public void Schedule()
{
    // Arrange
    var requests = new List<IAppointment>{AppointmentBuilder.CreateDefault()
        .WithPatientName("Dixie")
        .WithProposedTimes(new DateTime(1953,4,12))};

    // Act
    var results = MedicalScheduler.Schedule(requests);

    // Assert
    Assert.IsTrue(results.Any());
    // results.Should().HaveCount(1); // If you're using FluentAssertions
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文