Skip to content

Instantly share code, notes, and snippets.

@johlju
Created May 27, 2020 16:44
Show Gist options
  • Save johlju/8578bf8cf693bbb2dc8f818820323759 to your computer and use it in GitHub Desktop.
Save johlju/8578bf8cf693bbb2dc8f818820323759 to your computer and use it in GitHub Desktop.
Pester 5 MockUp Fails on SwitchParamater
BeforeAll {
Remove-Module -Name 'DscResource.Common' -Force
New-Module -Name 'DscResource.Common' -ScriptBlock {
function Assert-Module
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$ModuleName,
[Parameter()]
[System.Management.Automation.SwitchParameter]
$ImportModule
)
if (-not (Get-Module -Name $ModuleName -ListAvailable))
{
$errorMessage = $script:localizedData.ModuleNotFound -f $ModuleName
New-ObjectNotFoundException -Message $errorMessage
}
if ($ImportModule)
{
Import-Module -Name $ModuleName
}
}
} | Import-Module
$script:moduleName = 'DscResource.Common'
}
Describe 'Assert-Module' {
BeforeAll {
$testModuleName = 'TestModule'
}
Context 'When module is available' {
BeforeAll {
Mock -CommandName Import-Module -ModuleName 'DscResource.Common'
Mock -CommandName Get-Module -MockWith {
return @{
Name = $testModuleName
}
} -ModuleName 'DscResource.Common'
}
Context 'When module should be imported' {
It 'Should not throw an error' {
{ Assert-Module -ModuleName $testModuleName -ImportModule } | Should -Not -Throw
}
It 'Should call the expected mocks' {
Should -Invoke -CommandName Import-Module -Exactly -Times 1 -Scope Context
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment