繁体   English   中英

如何对此自定义命令行开关进行单元测试

[英]How do I unit test this custom commandlet

如果没有提供参数,我无法弄清楚如果这个命令行开关失败,如何进行单元测试。

[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
    }
}
}

我尝试了以下方法但是,它没有因为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));
}
}

好的,我想我看到了这个问题。 你的参数是一个int,int不是null,它们永远不会是空的。 我建议验证参数的值不为零,或者使它成为一个int? 或者Nullable

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM