【问题标题】:How do I unit test this custom commandlet如何对这个自定义命令行开关进行单元测试
【发布时间】:2013-07-16 16:16:51
【问题描述】:

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

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

【问题讨论】:

  • 您的异常可能是在try..catchtry..catch之外的其他方法之一中引发的吗?
  • @Simon:我对其进行了调试以查看代码流......以及为什么我确信它在 //Do processing 中失败
  • 是的.. 但是您的代码是否进入try..catch,然后在// Do processing 中失败?还是在到达try..catch 之前失败?
  • @Simon: 在我进入result.MoveNext() 语句后,//Do 处理被执行。所以基本上它在进入try..catch后失败了。正如我从博客中了解到的那样,只有在调用 MoveNext() 之后才能执行 cmdlet。如果我误解了什么,请告诉我。
  • 好的,我想我明白了。您的参数是int,int 不为空,并且它们永远不会为空。我建议验证参数的值不为零,或将其设为int?Nullable<int>

标签: c# unit-testing c#-4.0 testing powershell


【解决方案1】:

好的,我想我明白了。您的参数是一个 int,int 不为空,并且它们永远不会为空。我建议验证参数的值不为零,还是将其设为 int?或可空

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 2017-02-18
    相关资源
    最近更新 更多