[英]How do I unit test this custom commandlet
I am unable to figure out how to unit test if this commandlet will fail, if no arguments are provided to it. 如果没有提供参数,我无法弄清楚如果这个命令行开关失败,如何进行单元测试。
[Cmdlet(VerbsCommon.Move, "SomeResource")]
public class MoveSomeResource : Cmdlet
{
private int _id;
[Parameter(Position = 0, Mandatory = true)]
[ValidateNotNullOrEmpty]
public int ID
{
get { return _id; }
set { _id = value; }
}
protected override void ProcessRecord()
{
string text = string.Format("Move Resource {0} ", this._id);
//Do something
if (ShouldProcess(text, action))
{
//Do processing
}
}
}
I tried the following method but, it does not fails due to ValidateNotNullOrEmpty error rather it executes the part in //Do Processing and fails there . 我尝试了以下方法但是,它没有因为ValidateNotNullOrEmpty错误而失败,而是执行// Do Processing中的部分并在那里失败。
[TestMethod]
public void TestMoveBluh()
{
MoveSomeResource cmd = new MoveSomeResource();
IEnumerator result = cmd.Invoke().GetEnumerator();
try
{
result.MoveNext();
}
catch (Exception e)
{
Assert.IsInstanceOfType(e, typeof(ArgumentException));
}
}
Ok, I think I see the issue. 好的,我想我看到了这个问题。 Your parameter is an int, int's aren't null, and they are never empty. 你的参数是一个int,int不是null,它们永远不会是空的。 I suggest validating that the Value of the parameter is not zero, or to make it an int? 我建议验证参数的值不为零,或者使它成为一个int? or Nullable 或者Nullable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.