【发布时间】: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