c# .Net Worker 服务中的测试用例方法
Test Cases Method in c# .Net Worker Service
我刚开始使用 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,但如果您不想这样做,
你可以让它更容易测试。
如果您看一下,验证 DTO 并不是 TwilioHandler
真正关心的问题(至少不应该是)。那么为什么不把它分解成它自己的 class 呢?
然后您可以轻松测试 class / 方法,而无需模拟 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));
}
对于其他属性,您将编写相应的测试。
我刚开始使用 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,但如果您不想这样做, 你可以让它更容易测试。
如果您看一下,验证 DTO 并不是 TwilioHandler
真正关心的问题(至少不应该是)。那么为什么不把它分解成它自己的 class 呢?
然后您可以轻松测试 class / 方法,而无需模拟 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));
}
对于其他属性,您将编写相应的测试。