lan*_*der 5 c# testing unit-testing mocking
我是嘲笑/测试的新手,想要知道测试时应该达到什么水平.例如,在我的代码中,我有以下对象:
public class RuleViolation
{
public string ErrorMessage { get; private set; }
public string PropertyName { get; private set; }
public RuleViolation( string errorMessage )
{
ErrorMessage = errorMessage;
}
public RuleViolation( string errorMessage, string propertyName )
{
ErrorMessage = errorMessage;
PropertyName = propertyName;
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个相对简单的对象.所以我的问题是:
是否需要进行单元测试?
如果它做我测试什么,怎么做?
谢谢
我想说可能不会。您可能想要验证是否极其重要的唯一一件事是访问修饰符:
public string ErrorMessage { get; private set; }
public string PropertyName { get; private set; }
Run Code Online (Sandbox Code Playgroud)
如果类外部的代码无法修改它们确实非常重要,那么这可能是我唯一会尝试和验证的事情。
以下是获取属性中的访问器的方法:
class Program
{
static void Main(string[] args)
{
var property = typeof(Test).GetProperty("Accessor");
var methods = property.GetAccessors();
}
}
public class Test
{
public string Accessor
{
get;
private set;
}
}
Run Code Online (Sandbox Code Playgroud)
随着property.GetAccessors();
您可以查看设置器是否在那里。如果是,则 setter 是公开的。(您还可以使用 IsPrivate 和 IsPublic 属性来验证其他访问器)。
| 归档时间: |
|
| 查看次数: |
1765 次 |
| 最近记录: |