简体   繁体   中英

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 .

[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. I suggest validating that the Value of the parameter is not zero, or to make it an int? or Nullable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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