Skip to content

Commit 65ea407

Browse files
committed
Refactor Test-SqlDscBackupFile to assert exceptions for invalid and non-existent backup files
1 parent d723cab commit 65ea407

File tree

1 file changed

+4
-39
lines changed

1 file changed

+4
-39
lines changed

tests/Integration/Commands/Test-SqlDscBackupFile.Integration.Tests.ps1

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -107,51 +107,16 @@ Describe 'Test-SqlDscBackupFile' -Tag @('Integration_SQL2017', 'Integration_SQL2
107107
}
108108
}
109109

110-
It 'Should return false or throw an error for an invalid backup file' {
111-
# SQL Server may either return false or throw an exception for invalid backup files
112-
# depending on the level of corruption/invalidity
113-
$result = $null
114-
$threwError = $false
115-
116-
try
117-
{
118-
$result = Test-SqlDscBackupFile -ServerObject $script:serverObject -BackupFile $script:invalidBackupFile
119-
}
120-
catch
121-
{
122-
$threwError = $true
123-
$_.FullyQualifiedErrorId | Should -Be 'TSBF0004,Test-SqlDscBackupFile'
124-
}
125-
126-
if (-not $threwError)
127-
{
128-
$result | Should -BeFalse
129-
}
110+
It 'Should throw an error for an invalid backup file' {
111+
{ Test-SqlDscBackupFile -ServerObject $script:serverObject -BackupFile $script:invalidBackupFile } | Should -Throw -ErrorId 'TSBF0004,Test-SqlDscBackupFile'
130112
}
131113
}
132114

133115
Context 'When verifying a non-existent backup file' {
134-
It 'Should return false or throw an error for a non-existent backup file' {
116+
It 'Should throw an error for a non-existent backup file' {
135117
$nonExistentFile = Join-Path -Path $script:backupDirectory -ChildPath 'NonExistentBackup_12345.bak'
136118

137-
# SQL Server may either return false or throw an exception for non-existent files
138-
$result = $null
139-
$threwError = $false
140-
141-
try
142-
{
143-
$result = Test-SqlDscBackupFile -ServerObject $script:serverObject -BackupFile $nonExistentFile
144-
}
145-
catch
146-
{
147-
$threwError = $true
148-
$_.FullyQualifiedErrorId | Should -Be 'TSBF0004,Test-SqlDscBackupFile'
149-
}
150-
151-
if (-not $threwError)
152-
{
153-
$result | Should -BeFalse
154-
}
119+
{ Test-SqlDscBackupFile -ServerObject $script:serverObject -BackupFile $nonExistentFile } | Should -Throw -ErrorId 'TSBF0004,Test-SqlDscBackupFile'
155120
}
156121
}
157122

0 commit comments

Comments
 (0)