Skip to content

Commit

Permalink
Updated Test function to only catch SQL execution exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
David Reynolds authored and David Reynolds committed Aug 21, 2016
1 parent c127d90 commit dba7ab8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function Test-TargetResource
return $false
}
}
catch
catch [Microsoft.SqlServer.Management.PowerShell.SqlPowerShellSqlExecutionException]
{
Write-Verbose $_
return $false
Expand Down
24 changes: 17 additions & 7 deletions Tests/Unit/MSFT_xSQLServerScript.Test.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<#
.Synopsis
Automated unit test for MSFT_xSQLServerScript DSC Resource
.SYNOPSIS
Automated unit test for MSFT_xSQLServerScript DSC Resource
#>


Expand All @@ -25,6 +25,18 @@ $TestEnvironment = Initialize-TestEnvironment `

#endregion HEADER

Add-Type -ErrorAction SilentlyContinue -TypeDefinition @'
namespace Microsoft.SqlServer.Management.PowerShell
{
public class SqlPowerShellSqlExecutionException : System.Exception
{
public SqlPowerShellSqlExecutionException()
{
}
}
}
'@

# Begin Testing
try
{
Expand Down Expand Up @@ -56,11 +68,9 @@ try
}

It 'Test method returns false' {
$throwMessage = "Failed to run SQL Script"

Mock -CommandName Invoke-SqlCmd -MockWith { Throw $throwMessage }
Mock -CommandName Invoke-SqlCmd -MockWith { throw New-Object Microsoft.SqlServer.Management.PowerShell.SqlPowerShellSqlExecutionException}

Test-TargetResource @testParameters | should be $false
Test-TargetResource @testParameters | should be $false
}

It 'Set method calls Invoke-SqlCmd' {
Expand Down Expand Up @@ -182,7 +192,7 @@ try
}

It 'Test method throws' {
Test-TargetResource @testParameters | should be $false
{ Test-TargetResource @testParameters } | should throw $throwMessage
}

It 'Set method throws' {
Expand Down

0 comments on commit dba7ab8

Please sign in to comment.