Updating tests for multiuse

This commit is contained in:
Wes Carroll
2019-07-11 18:14:32 -04:00
parent e930f996d6
commit 5eae4857cf
5 changed files with 247 additions and 266 deletions
+36 -42
View File
@@ -1,66 +1,60 @@
#Requires -Modules Pester
$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
$global:here = (Split-Path -Parent $MyInvocation.MyCommand.Path)
$script:function = ((Split-Path -leaf $MyInvocation.MyCommand.Path).Split('.'))[0]
Describe $file.BaseName -Tag 'Unit' {
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
Describe $script:function -Tag 'Unit', 'Source', 'Built' {
BeforeAll {
$script:ScriptBlock = (Get-Command $script:function).ScriptBlock
}
Mock -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest {
return "3b687246-ac63-40da-9a59-b99863769eb0.928a122b-1763-4664-ad37-cc00bb883f2f"
return (Get-Content "$global:here\Mocks\TaskId.txt")
}
Mock -ModuleName ZertoApiWrapper -CommandName get-zertovpg {
return @{vpgIdentifier = "dddf2fa8-79e2-4e4f-a83b-f66676afea64"}
Mock -ModuleName ZertoApiWrapper -CommandName get-zertovpg -ParameterFilter { $VpgName -eq "MyVpg" } {
return (Get-Content "$global:here\Mocks\VPGInfo.json" -Raw | ConvertFrom-Json)
}
Context "$($file.BaseName)::Parameter Unit Tests" {
it "Has a parameter for the VpgName that is Mandatory" {
Get-Command $file.BaseName | Should -HaveParameter vpgName -Mandatory -Type String
Context "$($script:function)::Parameter Unit Tests" {
It "Has a parameter for the VpgName that is Mandatory" {
Get-Command $script:function | Should -HaveParameter vpgName -Mandatory -Type String
}
it "Has a parameter for the CheckpointName that is Mandatory" {
Get-Command $file.BaseName | Should -HaveParameter CheckpointName -Mandatory -Type String
It "Has a parameter for the CheckpointName that is Mandatory" {
Get-Command $script:function | Should -HaveParameter CheckpointName -Mandatory -Type String
}
it "Throws and error when an empty or null checkpointName is specified" {
{Checkpoint-ZertoVpg -vpgName "MyVpg" -checkpointName ""} | Should -Throw
{Checkpoint-ZertoVpg -vpgName "MyVpg" -checkpointName $null} | Should -Throw
It "Throws and error when an empty or null checkpointName is specified" {
{ Checkpoint-ZertoVpg -vpgName "MyVpg" -checkpointName "" } | Should -Throw
{ Checkpoint-ZertoVpg -vpgName "MyVpg" -checkpointName $null } | Should -Throw
}
it "Throws an error when an empty or null vpgName is specified" {
{Checkpoint-ZertoVpg -vpgName "" -checkpointName "MyCheckPoint"} | Should -Throw
{Checkpoint-ZertoVpg -vpgName $null -checkpointName "MyCheckPoint"} | Should -Throw
It "Throws an error when an empty or null vpgName is specified" {
{ Checkpoint-ZertoVpg -vpgName "" -checkpointName "MyCheckPoint" } | Should -Throw
{ Checkpoint-ZertoVpg -vpgName $null -checkpointName "MyCheckPoint" } | Should -Throw
}
it "Does not support 'SupportsShouldProcess'" {
Get-Command $file.BaseName | Should -Not -HaveParameter WhatIf
Get-Command $file.BaseName | Should -Not -HaveParameter Confirm
$file | Should -Not -FileContentMatch 'SupportsShouldProcess'
$file | Should -Not -FileContentMatch '\$PSCmdlet\.ShouldProcess\(.+\)'
It "Does not support 'SupportsShouldProcess'" {
Get-Command $script:function | Should -Not -HaveParameter WhatIf
Get-Command $script:function | Should -Not -HaveParameter Confirm
$script:ScriptBlock | Should -not -match 'SupportsShouldProcess'
$script:ScriptBlock | Should -not -match '\$PSCmdlet\.ShouldProcess\(.+\)'
}
}
Context "$($file.BaseName)::Function Unit Tests" {
Context "$($script:function)::Function Unit Tests" {
it "should return a not null or empty string" {
It "should return a not null or empty string" {
$results = Checkpoint-ZertoVpg -vpgName "MyVpg" -checkpointName "My Checkpoint Name"
$results | should -not -BeNullOrEmpty
$results | should -BeOfType "String"
$results | should -BeExactly "3b687246-ac63-40da-9a59-b99863769eb0.928a122b-1763-4664-ad37-cc00bb883f2f"
$results | Should -not -BeNullOrEmpty
$results | Should -BeOfType "String"
$results | Should -BeExactly "7e79035e-fb8c-47fe-815c-12ddd41708e6.3e4cdd0d-1064-4022-921f-6265ad6d335a"
}
It "does not return anything when a invalid VPG is defined" {
$results = Checkpoint-ZertoVpg -vpgName "DoesNotExist" -checkpointName "My Checkpoint Name"
$results | Should -Be "Cannot find VPG named DoesNotExist. Please check the name and try again."
}
Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest
Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Get-ZertoVpg
Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -Exactly 2
Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Get-ZertoVpg -Exactly 1
}
}
}