Created
August 14, 2020 18:05
-
-
Save 1RedOne/4f60a5739c40a7541006cce153fc4194 to your computer and use it in GitHub Desktop.
PowerShell Function to handle Moq Setup and Verifies for you!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Synopsis | |
Creates your Moq.Setups for you! | |
.DESCRIPTION | |
Provide a method signature to receive an example of a basic, lazy Mock for the method | |
.EXAMPLE | |
$myMethodSignature = " | |
GetDeploymentStatus( | |
string someToken, | |
int someIntValue = 10, | |
bool someBoolValue = false, | |
TimeSpan delayLookup = default, | |
CancellationToken cancellationToken | |
)" | |
New-MoqMethodConfiguration $myMethodSignature | |
.Setup(m=>m. | |
GetDeploymentStatus(It.IsAny<string>(),It.IsAny<int>(),It.IsAny<bool>(),It.IsAny<TimeSpan>(),It.IsAny<CancellationToken>())).Returns... | |
.EXAMPLE | |
Another example of how to use this cmdlet | |
#> | |
Function New-MoqMethodConfiguration{ | |
[CmdletBinding()] | |
param( | |
[String] | |
$methodSignature, | |
[Switch] | |
$Verify = $false | |
) | |
$outputs = New-Object System.Collections.ArrayList | |
$methodName = $methodSignature.Split('(')[0] | |
Write-Verbose "found method of name $methodName" | |
$parms= $methodSignature.Split('(')[1..10].Split("`n").Trim().Replace(')','') | |
Write-Verbose "found $($parms.Count) parameters" | |
ForEach($parm in $parms){ | |
if($parm.Length -le 0){continue} | |
Write-Verbose "processing $parm " | |
$outputs.Add("It.IsAny<$($parm.Split()[0])>()") | Out-Null | |
} | |
$paramMatcher = $outputs -join "," | |
if ($Verify){ | |
Write-output ".Verify(m=>m.$($methodName)($paramMatcher), Times.Once)" | |
} | |
else{ | |
Write-output ".Setup(m=>m.$($methodName)($paramMatcher)).Returns..." | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment