单元测试以检查未使用的属性
Unit test to check for unused properties
我有一个函数遍历 class 的属性,并用模板中的相同名称替换两个美元符号之间的关键字。
一个class的例子:
public class FeedMessageData : IMailObject
{
public string Username { get; private set;}
public string SubscriptionID { get; private set; }
public string MessageTime { get; private set; }
public string Subject { get; private set; }
public FeedMessageData(string username, string subscriptionID, DateTime messageTime)
{
this.Username = username;
this.SubscriptionID = subscriptionID;
this.MessageTime = messageTime.ToShortDateString();
this.Subject = "Feed " + DateTime.Now + " - SubscriptionID: " + this.SubscriptionID;
}
}
这是用属性替换模板的函数:
private string mergeTemplate(string template, IMailObject mailObject)
{
Regex parser = new Regex(@"$(?:(?<operation>[\w\-\,\.]+) ){0,1}(?<value>[\w\-\,\.]+)$", RegexOptions.Compiled);
var matches = parser.Matches(template).Cast<Match>().Reverse();
foreach (var match in matches)
{
string operation = match.Groups["operation"].Value;
string value = match.Groups["value"].Value;
var propertyInfo = mailObject.GetType().GetProperty(value);
if (propertyInfo == null)
throw new TillitException(String.Format("Could not find '{0}' in object of type '{1}'.", value, mailObject));
object dataValue = propertyInfo.GetValue(mailObject, null);
template = template.Remove(match.Index, match.Length).Insert(match.Index, dataValue.ToString());
}
return template;
}
我希望创建一个写入控制台的单元测试,以及模板中未使用的可能属性。例如,如果模板中没有 $SubscriptionID$。我已经尝试使用 PropertyInfo,它为我提供了 class 的属性,但我如何使用此信息来检查它们是否已在模板中使用?
Moq (https://github.com/moq/moq4/wiki) 提供验证 property/method 访问的方法。
请按照 this link 上的教程了解更多详细信息。要验证您的属性是否在您的模板中被使用,您可以使用 VerifyGet
方法,示例如下:
[Fact]
public void VerifyAllPropertiesHaveBeenConsumedInTemplate()
{
var mockMailObject = new Mock<IMailObject>();
var template = "yourTemplateOrMethodThatReturnsYourTemplate";
var result = mergeTemplate(template, mockMailObject.Object);
mockMailObject.VerifyGet(m => m.Username, Times.Once);
mockMailObject.VerifyGet(m => m.SubscriptionID, Times.Once);
mockMailObject.VerifyGet(m => m.MessageTime, Times.Once);
mockMailObject.VerifyGet(m => m.Subject, Times.Once);
}
我有一个函数遍历 class 的属性,并用模板中的相同名称替换两个美元符号之间的关键字。
一个class的例子:
public class FeedMessageData : IMailObject
{
public string Username { get; private set;}
public string SubscriptionID { get; private set; }
public string MessageTime { get; private set; }
public string Subject { get; private set; }
public FeedMessageData(string username, string subscriptionID, DateTime messageTime)
{
this.Username = username;
this.SubscriptionID = subscriptionID;
this.MessageTime = messageTime.ToShortDateString();
this.Subject = "Feed " + DateTime.Now + " - SubscriptionID: " + this.SubscriptionID;
}
}
这是用属性替换模板的函数:
private string mergeTemplate(string template, IMailObject mailObject)
{
Regex parser = new Regex(@"$(?:(?<operation>[\w\-\,\.]+) ){0,1}(?<value>[\w\-\,\.]+)$", RegexOptions.Compiled);
var matches = parser.Matches(template).Cast<Match>().Reverse();
foreach (var match in matches)
{
string operation = match.Groups["operation"].Value;
string value = match.Groups["value"].Value;
var propertyInfo = mailObject.GetType().GetProperty(value);
if (propertyInfo == null)
throw new TillitException(String.Format("Could not find '{0}' in object of type '{1}'.", value, mailObject));
object dataValue = propertyInfo.GetValue(mailObject, null);
template = template.Remove(match.Index, match.Length).Insert(match.Index, dataValue.ToString());
}
return template;
}
我希望创建一个写入控制台的单元测试,以及模板中未使用的可能属性。例如,如果模板中没有 $SubscriptionID$。我已经尝试使用 PropertyInfo,它为我提供了 class 的属性,但我如何使用此信息来检查它们是否已在模板中使用?
Moq (https://github.com/moq/moq4/wiki) 提供验证 property/method 访问的方法。
请按照 this link 上的教程了解更多详细信息。要验证您的属性是否在您的模板中被使用,您可以使用 VerifyGet
方法,示例如下:
[Fact]
public void VerifyAllPropertiesHaveBeenConsumedInTemplate()
{
var mockMailObject = new Mock<IMailObject>();
var template = "yourTemplateOrMethodThatReturnsYourTemplate";
var result = mergeTemplate(template, mockMailObject.Object);
mockMailObject.VerifyGet(m => m.Username, Times.Once);
mockMailObject.VerifyGet(m => m.SubscriptionID, Times.Once);
mockMailObject.VerifyGet(m => m.MessageTime, Times.Once);
mockMailObject.VerifyGet(m => m.Subject, Times.Once);
}