Skip to content

Instantly share code, notes, and snippets.

@SQLDBAWithABeard
Last active June 27, 2017 14:10
Show Gist options
  • Save SQLDBAWithABeard/57d9802d15befa551e1d5101557c2bd4 to your computer and use it in GitHub Desktop.
Save SQLDBAWithABeard/57d9802d15befa551e1d5101557c2bd4 to your computer and use it in GitHub Desktop.
Why he no mock
$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" {
}
}
}
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/ "
}
}
$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
}
}
$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
}
}
@SQLDBAWithABeard
Copy link
Author

So it is a scoping thing as the Working.unit.Tests.ps1 works but I do not understand why and I also do not understand how to fix it

@SQLDBAWithABeard
Copy link
Author

Ok I fixed it - InModuleScope fixed it - still don't quite understand why!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment