0

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?

2
  • Please clarify: In C#, you're creating a ConvertFromCmdlet, but in F# you're creating a ConvertFromJwtCommand. Are those intended to be the same thing? Both of your F# snippets correctly set the Jwt property, so I suspect the problem you're having occurs elsewhere. It might help to see more of your code. Commented Oct 7, 2023 at 17:23
  • Hi @BrianBerns, I added now the header code of the PowerShell function. In the original C# code, I used XUnit (it was a few years ago) and now with F# I use NUnit. Commented Oct 8, 2023 at 14:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.