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?
ConvertFromCmdlet, but in F# you're creating aConvertFromJwtCommand. Are those intended to be the same thing? Both of your F# snippets correctly set theJwtproperty, so I suspect the problem you're having occurs elsewhere. It might help to see more of your code.