Help - turning my script into Pester

ZM 211 Reputation points
2022-04-25T12:33:11.023+00:00

hi im sturggling with pester and i wanted to ask if you could help me out.

i want to turn this script below in pester, would you be able to do that?

[Cmdletbinding()]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$Regex
)

$Result = [Guid]::empty
[Guid]::TryParse("$Regex", [System.Management.Automation.PSReference]$Result)

if (-not [Guid]::TryParse("$Regex", [System.Management.Automation.PSReference]$Result))
{
Write-error "The Guid entered is incorrect"
}

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-04-25T18:29:58.487+00:00

    If you're struggling with understanding Pester you should consider ordering this book: pesterbook, or you can find many examples of using Pester on the web. The problem with using the web is that the things you find there may not be what you want to accomplish, or they cover a narrow topic and you have to go look for more information. The book, at least, puts in all (or most of it) in one place.

    But I don't understand what you want to do here. Pester would probably be used to write tests that cover the data you expect to be submitted to your function and the results returned from the function. In other words, Pester would load your module/function and then calling that function with data and examining the result, comparing it against your stated expectations. If the function is called using a valid GUID you'd expect a value of $TRUE to be returned. If it was called using an invalid GUID your function should return $FALSE.

    However, your function, when called with an invalid GUID returns $FALSE in the "success" stream AND a non-terminating exception in the "error" stream. To test that you'd need to be sure to set the default error action in those cases to "STOP" so you an intercept the exception (or change the design of the function to not generate the non-terminating error -- OR change the Write-Error cmdlet to a "Throw" statement to cause a terminating exception).

    In your post you said that you "want to turn this script below in pester". What does that mean? Did you mean "test" instead or "turn"?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.