Skip to content

Commit

Permalink
Warnings in Tests Fix (#1230)
Browse files Browse the repository at this point in the history
* Get latest version of Pester from Gallery

* Altered to output failed only

* Fixed test to not error but still check write-warning is called

* Fixed so tests dont error and correctly test function
  • Loading branch information
SQLDBAWithABeard authored and potatoqualitee committed May 16, 2017
1 parent 0028f0d commit 5921931
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Expand Up @@ -4,13 +4,13 @@
skip_commits:
message: /updated readme.*/
install:
- cinst pester
build: false
test_script:
# Test with native PS version
- ps: Install-PackageProvider NuGet -MinimumVersion '2.8.5.201' -Force
- ps: Import-PackageProvider NuGet -MinimumVersion '2.8.5.201' -Force
- ps: Install-Module -Name PSScriptAnalyzer -RequiredVersion 1.6.0 -Repository PSGallery -Force
- ps: INstall-Module -Name Pester -Repository PSGallery -Force
- ps: . .\Tests\appveyor.pester.ps1
# Test with PS version 3
- ps: powershell.exe -version 3.0 -executionpolicy bypass -noprofile -file .\Tests\appveyor.pester.ps1
Expand Down
23 changes: 20 additions & 3 deletions tests/Get-DirectoryRestoreFile.Tests.ps1
Expand Up @@ -14,13 +14,30 @@ if($env:APPVEYOR_REPO_BRANCH -and $env:APPVEYOR_REPO_BRANCH -notlike "master")

$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace('.Tests.', '.')
Import-Module $PSScriptRoot\..\internal\$sut -Force

Describe "Get-DirectoryRestoreFile Unit Tests" -Tag 'Unittests'{
Context "Test Path handling" {
Mock Test-Path {$false}
Mock Write-Warning {
throw}
It "Should throw on an invalid Path"{
Mock Test-Path {$false}
{Get-DirectoryRestoreFile -Path c:\temp\} | Should Throw
{ Get-DirectoryRestoreFile -Path c:\temp\} | Should Throw
}
It 'Calls Test-Path Mock Once' {
$assertMockParams = @{
'CommandName' = 'Test-Path'
'Times' = 1
'Exactly' = $true
}
Assert-MockCalled @assertMockParams
}
It 'Calls Write-Warning Mock Once' {
$assertMockParams = @{
'CommandName' = 'Write-Warning'
'Times' = 1
'Exactly' = $true
}
Assert-MockCalled @assertMockParams
}
}
Context "Returning Files from one folder" {
New-item "TestDrive:\backups\" -ItemType directory
Expand Down
31 changes: 25 additions & 6 deletions tests/Get-XpDirTreeRestoreFile.Tests.ps1
Expand Up @@ -24,15 +24,34 @@ Describe "Get-XpDirTree Unit Tests" -Tag 'Unittests'{
mock Test-SqlPath {$true}

Context "Test Connection and User Rights" {
Mock Test-SQLPath {$false}
Mock Write-Warning {Throw} ## Need to mock the warnign to stop the error in tests

It "Should throw on an invalid SQL Connection" {
#mock Test-SQLConnection {(1..12) | %{[System.Collections.ArrayList]$t += @{ConnectSuccess = $false}}}
Mock Connect-SQLServer {throw}
{Get-XpDirTreeRestoreFile -path c:\dummy -sqlserver bad\bad} | Should Throw
Mock Connect-SQLServer {Throw} ## Mock the Connect call to hit the catch inside the function
#mock Test-SQLConnection {(1..12) | %{[System.Collections.ArrayList]$t += @{ConnectSuccess = $false}}}
{Get-XpDirTreeRestoreFile -path c:\dummy -sqlserver bad\bad} | Should Throw
}
It "Should throw if SQL Server can't see the path" {
$srv = New-Object Microsoft.SQLServer.Management.Smo.Server dummy ## Mock the server object else throws on Connect and doesnt test path
{Get-XpDirTreeRestoreFile -path c:\dummy -sqlserver $srv} | Should Throw
}
It "Should throw if SQL Server can't see the path" {
Mock Test-SQLPath {$false}
{Get-XpDirTreeRestoreFile -path c:\dummy -sqlserver bad\bad} | Should Throw
It 'Calls Connect-SQLServer Mock Once' {
$assertMockParams = @{
'CommandName' = 'Connect-SQLServer'
'Times' = 1
'Exactly' = $true
}
Assert-MockCalled @assertMockParams ## Ensure Mocks are called
}
It 'Calls Test-SqlPath Mock Once' {
$assertMockParams = @{
'CommandName' = 'Test-SqlPath'
'Times' = 1
'Exactly' = $true
}
Assert-MockCalled @assertMockParams ## Ensure Mocks are called
}
}
Context "Non recursive filestructure" {
$array = (@{subdirectory='full.bak';depth=1;file=1},
Expand Down
2 changes: 1 addition & 1 deletion tests/appveyor.pester.ps1
Expand Up @@ -20,7 +20,7 @@ param([switch]$Finalize)

Import-Module Pester
Set-Variable ProgressPreference -Value SilentlyContinue
Invoke-Pester -Quiet -Path "$ProjectRoot\Tests" -OutputFormat NUnitXml -OutputFile "$ProjectRoot\$TestFile" -PassThru |
Invoke-Pester -Show Failed -Path "$ProjectRoot\Tests" -OutputFormat NUnitXml -OutputFile "$ProjectRoot\$TestFile" -PassThru |
Export-Clixml -Path "$ProjectRoot\PesterResults$PSVersion.xml"
}

Expand Down

0 comments on commit 5921931

Please sign in to comment.