Last active
June 27, 2017 14:10
-
-
Save SQLDBAWithABeard/57d9802d15befa551e1d5101557c2bd4 to your computer and use it in GitHub Desktop.
Why he no mock
This file contains 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
$script:ModuleName = 'SQLDiagAPI' | |
# Removes all versions of the module from the session before importing | |
Get-Module $ModuleName | Remove-Module | |
$ModuleBase = Split-Path -Parent $MyInvocation.MyCommand.Path | |
# For tests in .\Tests subdirectory | |
if ((Split-Path $ModuleBase -Leaf) -eq 'Tests') { | |
$ModuleBase = Split-Path $ModuleBase -Parent | |
} | |
Import-Module $ModuleBase\$ModuleName.psd1 -ErrorAction Stop #| Out-Null | |
InModuleScope -ModuleName SQLDiagAPI { | |
Describe "Get-SQLDiagRecommendations" -Tags Build , Unit { | |
Context "Requirements" { | |
BeforeAll { | |
Mock Get-ChildItem {} | |
Mock Write-Warning {"Warning"} | |
} | |
It "Should throw a warning if there is no API Key Environment Variable" { | |
Get-SQLDiagRecommendations -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Should Be "Warning" | |
} | |
It 'Checks the Mock was called for Get-ChildItem' { | |
$assertMockParams = @{ | |
'CommandName' = 'Get-ChildItem' | |
'Times' = 1 | |
'Exactly' = $true | |
} | |
Assert-MockCalled @assertMockParams | |
} | |
It 'Checks the Mock was called for Write-Warning' { | |
$assertMockParams = @{ | |
'CommandName' = 'Write-Warning' | |
'Times' = 1 | |
'Exactly' = $true | |
} | |
Assert-MockCalled @assertMockParams | |
} | |
} | |
Context "Input" { | |
} | |
Context "Execution" { | |
} | |
Context "Output" { | |
} | |
} | |
} |
This file contains 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
function Get-SQLDiagRecommendations { | |
[cmdletbinding()] | |
Param() | |
if(!(Get-ChildItem Env:MS_SQLDiag_APIKey -ErrorAction SilentlyContinue)){ | |
Write-Warning "You have not created an Environment Variable MS_SQLDiag_APIKey to hold the APIKey | |
You can do this using [Environment]::SetEnvironmentVariable(`"MS_SQLDiag_APIKey`", `"APIKEYGOESHERE`", `"User`") | |
You can get a key by following the steps here https://ecsapi.portal.azure-api.net/ " | |
} | |
} |
This file contains 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
$script:ModuleName = 'SQLDiagAPI' | |
# Removes all versions of the module from the session before importing | |
Get-Module $ModuleName | Remove-Module | |
$ModuleBase = Split-Path -Parent $MyInvocation.MyCommand.Path | |
# For tests in .\Tests subdirectory | |
if ((Split-Path $ModuleBase -Leaf) -eq 'Tests') { | |
$ModuleBase = Split-Path $ModuleBase -Parent | |
} | |
Import-Module $ModuleBase\$ModuleName.psd1 -PassThru -ErrorAction Stop | Out-Null | |
Describe "Get-SQLDiagRecommendations" -Tags Build , Unit{ | |
Context "Requirements" { | |
BeforeAll { | |
Mock Get-ChildItem {} | |
Mock Write-Warning {"Warning"} | |
} | |
It "Should throw a warning if there is no API Key Environment Variable"{ | |
Get-SQLDiagRecommendations -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Should Be "Warning" | |
} | |
It 'Checks the Mock was called for Get-ChildItem' { | |
$assertMockParams = @{ | |
'CommandName' = 'Get-ChildItem' | |
'Times' = 1 | |
'Exactly' = $true | |
} | |
Assert-MockCalled @assertMockParams | |
} | |
It 'Checks the Mock was called for Write-Warning' { | |
$assertMockParams = @{ | |
'CommandName' = 'Write-Warning' | |
'Times' = 1 | |
'Exactly' = $true | |
} | |
Assert-MockCalled @assertMockParams | |
} | |
} | |
Context "Input" { | |
} | |
Context "Execution" { | |
} | |
Context "Output" { | |
} | |
AfterAll { | |
Get-Module $ModuleName | Remove-Module | |
} | |
} |
This file contains 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
$script:ModuleName = 'SQLDiagAPI' | |
# Removes all versions of the module from the session before importing | |
Get-Module $ModuleName | Remove-Module | |
$ModuleBase = Split-Path -Parent $MyInvocation.MyCommand.Path | |
# For tests in .\Tests subdirectory | |
if ((Split-Path $ModuleBase -Leaf) -eq 'Tests') { | |
$ModuleBase = Split-Path $ModuleBase -Parent | |
} | |
Import-Module $ModuleBase\$ModuleName.psd1 -ErrorAction Stop #| Out-Null | |
Describe "Get-SQLDiagRecommendations" -Tags Build , Unit{ | |
function Get-SQLDiagRecommendations { | |
[cmdletbinding()] | |
Param() | |
if (!(Get-ChildItem Env:MS_SQLDiag_APIKey -ErrorAction SilentlyContinue)) { | |
Write-Warning "You have not created an Environment Variable MS_SQLDiag_APIKey to hold the APIKey | |
You can do this using [Environment]::SetEnvironmentVariable(`"MS_SQLDiag_APIKey`", `"APIKEYGOESHERE`", `"User`") | |
You can get a key by following the steps here https://ecsapi.portal.azure-api.net/ " | |
} | |
} | |
Context "Requirements" { | |
BeforeAll { | |
Mock Get-ChildItem {} | |
Mock Write-Warning {"Warning"} | |
} | |
It "Should throw a warning if there is no API Key Environment Variable"{ | |
Get-SQLDiagRecommendations -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Should Be "Warning" | |
} | |
It 'Checks the Mock was called for Get-ChildItem' { | |
$assertMockParams = @{ | |
'CommandName' = 'Get-ChildItem' | |
'Times' = 1 | |
'Exactly' = $true | |
} | |
Assert-MockCalled @assertMockParams | |
} | |
It 'Checks the Mock was called for Write-Warning' { | |
$assertMockParams = @{ | |
'CommandName' = 'Write-Warning' | |
'Times' = 1 | |
'Exactly' = $true | |
} | |
Assert-MockCalled @assertMockParams | |
} | |
} | |
Context "Input" { | |
} | |
Context "Execution" { | |
} | |
Context "Output" { | |
} | |
AfterAll { | |
Get-Module $ModuleName | Remove-Module | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok I fixed it - InModuleScope fixed it - still don't quite understand why!