diff --git a/Tests/Public/Export-ZertoVpg.Tests.ps1 b/Tests/Public/Export-ZertoVpg.Tests.ps1 index e46b59e..e733e93 100644 --- a/Tests/Public/Export-ZertoVpg.Tests.ps1 +++ b/Tests/Public/Export-ZertoVpg.Tests.ps1 @@ -1,53 +1,39 @@ #Requires -Modules Pester -#Region - Test Setup -$moduleFileName = "ZertoApiWrapper.psd1" -$here = (Split-Path -Parent $MyInvocation.MyCommand.Path).Replace("Tests", "ZertoApiWrapper") -$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") -$file = Get-ChildItem "$here\$sut" -$modulePath = $here -replace "Public", "" -$moduleFile = Get-ChildItem "$modulePath\$moduleFileName" -Get-Module -Name ZertoApiWrapper | Remove-Module -Force -Import-Module $moduleFile -Force -#EndRegion +$global:here = (Split-Path -Parent $MyInvocation.MyCommand.Path) +$global:function = ((Split-Path -leaf $MyInvocation.MyCommand.Path).Split('.'))[0] -Describe $file.BaseName -Tag 'Unit' { +Describe $global:function -Tag 'Unit', 'Source', 'Built' { - It "is valid Powershell (Has no script errors)" { - $contents = Get-Content -Path $file -ErrorAction Stop - $errors = $null - $null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors) - $errors | Should -HaveCount 0 - } + Context "$global:function::Parameter Unit Tests" { - Context "$($file.BaseName)::Parameter Unit Tests" { - it "has a mantatory string parameter for the output path" { - Get-Command $file.BaseName | Should -HaveParameter outputPath -Type String -Mandatory + $ParameterTestCases = @( + @{ParameterName = 'OutputPath'; Type = 'String'; Mandatory = $true; Validation = 'NotNullOrEmpty' } + @{ParameterName = 'vpgName'; Type = 'String[]'; Mandatory = $true; Validation = 'NotNullOrEmpty' } + @{ParameterName = 'allVpgs'; Type = 'Switch'; Mandatory = $true; Validation = $null } + ) + + It " parameter is of type, with correct validation" -TestCases $ParameterTestCases { + param($ParameterName, $Type, $Mandatory, $Validation) + Get-Command $global:function | Should -HaveParameter $ParameterName -Mandatory:$Mandatory -Type $Type } - it "has a non-mandatory string array parameter for vpgName(s) to export" { - Get-Command $file.BaseName | Should -HaveParameter vpgName -Type String[] -Mandatory - } + It " parameter has correct validation setting" -TestCases $ParameterTestCases { + param($ParameterName, $Validation) + Switch ($Validation) { + 'NotNullOrEmpty' { + $attrs = (Get-Command $global:function).Parameters[$ParameterName].Attributes + $attrs.Where{ $_ -is [ValidateNotNullOrEmpty] }.Count | Should -Be 1 + } - it "has a non-mandatory switch parameter to export all vpgs" { - Get-Command $file.BaseName | Should -HaveParameter allVpgs -Type Switch -Mandatory - } - - it "No defined vpgName or AllVpg switch should throw an error" { - {Export-ZertoVpg -outputPath "."} | Should -Throw - } - - it "Output path does not take null or empty string" { - {Export-ZertoVpg -outputPath "" -allVpgs} | Should -Throw - {Export-ZertoVpg -outputPath $null -allVpgs} | Should -Throw - } - - it "Vpg Name parameter does not take null or empty string" { - {Export-ZertoVpg -outputPath "." -vpgName = ""} | Should -Throw - {Export-ZertoVpg -outputPath "." -vpgName = $null} | Should -Throw + $null { + $attrs = (Get-Command $global:function).Parameters[$ParameterName].Attributes + $attrs.TypeId.Count | Should -Be 2 + } + } } } - Context "$($file.BaseName)::Function Unit Tests" { + Context "$($global:function)::Function Unit Tests" { Mock -ModuleName ZertoApiWrapper -CommandName Get-ZertoVpg { $returnObj = @{ VpgName = "HRIS" @@ -247,3 +233,6 @@ Describe $file.BaseName -Tag 'Unit' { Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Get-ZertoVpgSetting } } + +Remove-Variable -Name function -Scope Global +Remove-Variable -Name here -Scope Global