Skip to content

Commit

Permalink
Fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju committed Feb 28, 2023
1 parent 803cb4f commit c6b7d8d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
current state ([issue #1834](https://github.com/dsccommunity/SqlServerDsc/issues/1834)).
- `Set-TargetResource` was updated to correctly include or exclude a single
flag ([issue #1834](https://github.com/dsccommunity/SqlServerDsc/issues/1834)).
- SqlAudit
- Return the correct type for parameter `LogType` when calling method `Get()`.

## [16.0.0] - 2022-09-09

Expand Down
80 changes: 80 additions & 0 deletions tests/Unit/Classes/SqlReason.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
param ()

BeforeDiscovery {
try
{
if (-not (Get-Module -Name 'DscResource.Test'))
{
# Assumes dependencies has been resolved, so if this module is not available, run 'noop' task.
if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable))
{
# Redirect all streams to $null, except the error stream (stream 2)
& "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null
}

# If the dependencies has not been resolved, this will throw an error.
Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop'
}
}
catch [System.IO.FileNotFoundException]
{
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks build" first.'
}
}

BeforeAll {
$script:dscModuleName = 'SqlServerDsc'

$env:SqlServerDscCI = $true

Import-Module -Name $script:dscModuleName

$PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName
$PSDefaultParameterValues['Mock:ModuleName'] = $script:dscModuleName
$PSDefaultParameterValues['Should:ModuleName'] = $script:dscModuleName
}

AfterAll {
$PSDefaultParameterValues.Remove('InModuleScope:ModuleName')
$PSDefaultParameterValues.Remove('Mock:ModuleName')
$PSDefaultParameterValues.Remove('Should:ModuleName')

# Unload the module being tested so that it doesn't impact any other tests.
Get-Module -Name $script:dscModuleName -All | Remove-Module -Force

Remove-Item -Path 'env:SqlServerDscCI'
}

Describe 'SqlReason' -Tag 'SqlReason' {
Context 'When instantiating the class' {
It 'Should not throw an error' {
$script:mockSqlReasonInstance = InModuleScope -ScriptBlock {
[SqlReason]::new()
}
}

It 'Should be of the correct type' {
$mockSqlReasonInstance | Should -Not -BeNullOrEmpty
$mockSqlReasonInstance.GetType().Name | Should -Be 'SqlReason'
}
}

Context 'When setting an reading values' {
It 'Should be able to set value in instance' {
$script:mockSqlReasonInstance = InModuleScope -ScriptBlock {
$sqlReasonInstance = [SqlReason]::new()

$sqlReasonInstance.Code = 'SqlAudit:SqlAudit:Ensure'
$sqlReasonInstance.Phrase = 'The property Ensure should be "Present", but was "Absent"'

return $sqlReasonInstance
}
}

It 'Should be able read the values from instance' {
$mockSqlReasonInstance.Code | Should -Be 'SqlAudit:SqlAudit:Ensure'
$mockSqlReasonInstance.Phrase = 'The property Ensure should be "Present", but was "Absent"'
}
}
}

0 comments on commit c6b7d8d

Please sign in to comment.