【问题标题】:Test Cases Method in c# .Net Worker Servicec# .Net Worker 服务中的测试用例方法
【发布时间】:2021-02-03 11:34:38
【问题描述】:

我不熟悉在 c# 中进行单元测试,并试图了解如何在工作服务中使用 NUnit 进行测试用例,但我不知道如何复制它,因此它的测试与方法相同。这是一项在计算机后台运行和发送 SMS 的服务。

IMessagingInfo.cs - 这是我从中检索信息的模型

public interface IMessagingInfo
{
    public int? ID { get; set; }
    [DataType(DataType.Date)]
    DateTime? APPT_DATE { get; set; }

    string Location { get; set; }

    string TreatmentLocation { get; set; }
    
    string Ward { get; set; }

    string APPT_START { get; set; }

    Guid? ApptId { get; set; }

    string MobilePhone { get; set; }

}

TwilioHandler.cs:- 这是检查以确保数据库中的值正确且具有值的方法,以便服务不会在文本中发送空白信息。

public bool CheckModelFieldsValid(IMessagingInfo item)
    {

        if(item == null)
        {
            return false;
        }

        //implement this later as the mobile phone numbers have some that are null in DB
       // if (string.IsNullOrEmpty(item.MobilePhone))
       //{
       //   return false;
       // }
       // 

        if (item.APPT_START == null)
        {
            return false;
        }

        if (string.IsNullOrEmpty(item.TreatmentLocation))
        {
            return false;
        }

        if (string.IsNullOrEmpty(item.Location))
        {
            return false;
        }

        if(string.IsNullOrEmpty(item.Ward))
        {
            return false;
        }

        if (item.ApptId == null)
        {
            return false;
        }
        return true;
    }

这是我目前在 TwilioHandlerTests.cs 中的内容,我不了解如何设置框架和测试用例的单元测试逻辑。

public class Tests
{
    //dbcontext, TwilioHandler, TwilioAccount
    private Mock<DbContextConn> _MockDbContext;
    private Mock<IOptions<TwilioAccount>> _mockoptions;
    private Mock<ILogger<TwilioHandler>> _mockLogger;
    TwilioHandler _twh;

    private Mock<IMessagingInfo> _MessagingInfoMock = new Mock<IMessagingInfo>();


    [SetUp]
    public void Setup()
    {
        _MockDbContext = new Mock<DbContextConn>();
        _mockLogger = new Mock<ILogger<TwilioHandler>>();
        _mockoptions = new Mock<IOptions<TwilioAccount>>();
        //inmemorydatabase routine to add records 
    }
    [Test]
    public void CheckTwilioAccount_ModelFields_Valid_Test()
    {
     //want to replicate method here
    }

谢谢!

【问题讨论】:

  • 所以,基本上你想测试验证器。我越来越喜欢FluentValidation。使验证器非常好testable。 (我隶属于该项目)

标签: c# .net unit-testing testing nunit


【解决方案1】:

我已经建议使用 FluentValidation,但如果您不想这样做, 您可以使测试变得非常容易。

如果您看一下,验证 DTO 并不是 TwilioHandler 真正关心的问题(至少不应该如此)。那么为什么不把它放到它自己的类中呢?

然后您可以轻松测试该类/方法,而无需模拟 TwilioHandler 的任何依赖项。

然后您需要做的就是创建可能导致验证失败的案例*)并检查它们是否失败。反之亦然,创建一个应该通过的具有代表性的案例样本并检查它们是否通过。

*) 模拟这些情况的实例。例如具有item == null 的实例。

例子:

[Test]
void Validation_should_fail_if_item_is_null()
{
    bool expected = false;
// Assuming the Validator class is called "IMessagingInfoValidator", and there has been 
// setup a testsubject Instance named "ItemNullInstance".
    bool actual = new IMessagingInfoValidator().Validate(ItemNullInstance);
    Assert.That(actual, Is.EqualTo(expected));
}

对于其他属性,您将编写各自的测试。

【讨论】:

  • 我将遵循这一点并尝试更好地理解它。感谢您的回复,因为现在这似乎更容易理解了!
猜你喜欢
  • 2023-01-23
  • 1970-01-01
  • 1970-01-01
  • 2012-08-18
  • 2013-09-06
  • 2011-07-01
  • 2020-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多