Skip to content

Instantly share code, notes, and snippets.

@johlju
Last active May 16, 2020 09:34
Show Gist options
  • Save johlju/64c3dfd3364cfac14a4d9d825e7a08fd to your computer and use it in GitHub Desktop.
Save johlju/64c3dfd3364cfac14a4d9d825e7a08fd to your computer and use it in GitHub Desktop.
Notes when converting to Pester 5
* Should -Throw has a breaking change that it is no longer using `-contains` but instead `-like`
Expected an exception, with message 'Robocopy reported errors when copying files. Error code: 8.' to be thrown, but the message was 'System.InvalidOperationException: Robocopy reported errors when copying files. Error code: 8. (SQLCOMMON0012)'.
Solution, wasn't using the localized string (used hard-coded string) and then needed to use test helper Get-InvalidOperationRecord to get the correct string that contains System.InvalidOperationException (alternative is to use wildcards *)
Assert-MockCalled -> Should -Invoke (uses the same parameters, so backward compatible)
InModuleScope must be inside Describe-block.
Use it where it is needed, for example using localization variable.
No code outside Describe-, Context-, or It-block except if in a Before*-/After*-block. Well, except for variables that contain test cases, they must be outside Before*-blocks and It-blocks, but inside Describe- or Context-blocks.
Should -Throw -> Should -Throw -ExpectedMessage
`Should -Throw $null` will pass (for example wrong localized string) so must add a test to verify the correct
localized string is passes $errorMessage | Should -Not -BeNullOrEmpty
BeforeEach behaves different (better). Doesn't "override" mocks like it did in Pester 4. Must look over how mocking is done in these sceanrios. Not an issue when making context-blocks self-sustaining.
Param-block inside MockWith mmust be removed. Messes things up badly.
param
(
[Parameter()]
[System.String]
$ServerName,
[Parameter()]
[System.String]
$InstanceName
)
foreach() cannot be used since all code must be in Before*-blocks. Must use TestCases.
Using `Should Be` or `Should Throw` (without dash) throws error "ParameterBindingException: Parameter set cannot be resolved using the specified named parameters. One or more parameters issued cannot be used together or an insufficient number of parameters were provided."
If using test cases and have defined a parameter block and decorated the parameters with [Parameter()] it will fail.
Remove [Parameter()] (can remove the entire param-block, but then there is not backward compatibility with Pester 4)
param
(
[Parameter()]
[System.String]
$ParameterValue,
[Parameter()]
[System.String]
$DisplayName,
[Parameter()]
[System.String]
$Name
)
In Pester 5 it is possible to use Assert-MockCalled (Should -Invoke) without having declared a mock for the command (not sure it hits though, or just ignored if there is not mock decalred).
Got 5 failed test due to a bug https://github.com/pester/Pester/issues/1542
Describing SqlServerDsc.Common\Get-ServerProtocolObject
[+] Should return a ManagedComputer object with the correct values 14ms (13ms|0ms)
Tests completed in 26.08s
Tests Passed: 170, Failed: 5, Skipped: 0 NotRun: 0
This PR shows the issues in the code (for both Pester 4 and Pester 5) that with the help of Pester 5 was discovered: https://reviewable.io/reviews/dsccommunity/sqlserverdsc/1548
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment