mercredi 3 février 2021

Test Cases Method in c# .Net Worker Service

I'm new to doing unit testing in c# and trying to understand how to do test cases using NUnit in a worker service and I don't know how to replicate it so it tests the same as the method. This is a service that runs and sends SMS's in the background of a computer.

IMessagingInfo.cs - this is the model that I retrieve the information from

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: - this is the method that will check to ensure the values from the database are correct and have values so that the service doesn't send blank information in a text.

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;
    }

This is what I have so far in TwilioHandlerTests.cs where I dont understand the logistics of Unit testing on how to set up the framework and test cases.

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
    }

Thank you!

Aucun commentaire:

Enregistrer un commentaire