How do I unit test this custom cmdlet

I cannot figure out how to unit test if this batch file fails if it is not provided with arguments.

[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 crash due to a ValidateNotNullOrEmpty error, but it fulfills the role in // Do Processing and does not work 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)); } } 
+4
source share
1 answer

OK, I think I see the problem. Your parameter is int, int is not null, and they are never empty. I suggest checking that the parameter value is non-zero or make it int? or nullable

-one
source

Source: https://habr.com/ru/post/1491977/


All Articles