[英]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.