How do I pass a value in a Cmdlet class in F#?

50 Views Asked by At

I wrote a PowerShell function in F# and I would like to run NUnit tests on the functions. In C# I wrote these tests previously:

[Fact]
public void ConvertJWTTest()
{
  //-- Arrange
  var base64 = "{string}";
  //-- Act
  var cmdlet = new ConvertFromCmdlet()
  {
    JWT = base64
  };
  var actual = cmdlet.Invoke().OfType<Hashtable>().ToList();
  //-- Assert
  Assert.IsType<Hashtable>(actual[0]);
}

I struggle to setup the cmdlet variable in F#. The option to assign the value to the parameters results in a null. Same when I set it in the brackets directly. This is the cmdlet code:

[<Cmdlet("ConvertFrom", "Jwt")>]
type ConvertFromJwtCommand () =
    inherit PSCmdlet ()
    [<Parameter(
        Mandatory=true,
        ValueFromPipeline=true)>]
    [<ValidateNotNullOrEmpty>]
    [<ValidatePattern(@"(^[\w-]+\.[\w-]+\.[\w-]+$)")>]
    member val Jwt : string = String.Empty with get, set

This is my test. cmdletOutput returns a null.

[<Test>]
member this.CmdletTest () =
    
    let cmdletOutput = new ConvertFromJwtCommand ()
    cmdletOutput.Jwt <- base64

OR

    let cmdletOutput = new ConvertFromJwtCommand (Jwt = base64)

    // Act
    let result = cmdletOutput.Invoke().OfType<Hashtable>().ToList()
    Assert.AreEqual("string", result)

'Value cannot be null. (Parameter 'source')'.

How do I properly insert the value to the Cmdlet parameter?

0

There are 0 best solutions below

Related Questions in F#