From f642290b2d2548c182cf99b152e7e5f8838ce7f1 Mon Sep 17 00:00:00 2001 From: Conor Tolan Date: Sat, 23 Feb 2019 17:41:23 +0000 Subject: [PATCH] Tidied up the object creation in tests. --- Pester/Functions/Get-VMCOrg.tests.ps1 | 4 +- Pester/Functions/Get-VmcSddc.tests.ps1 | 78 ++++++++++++++++---------- 2 files changed, 49 insertions(+), 33 deletions(-) diff --git a/Pester/Functions/Get-VMCOrg.tests.ps1 b/Pester/Functions/Get-VMCOrg.tests.ps1 index 6f451aa..0726f5a 100644 --- a/Pester/Functions/Get-VMCOrg.tests.ps1 +++ b/Pester/Functions/Get-VMCOrg.tests.ps1 @@ -25,9 +25,7 @@ Describe "$functionName" -Tag 'Unit' { $object = [PSCustomObject]@{} $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } - $MockedArray = @() - $MockedArray += $MockedList - $MockedArray += $MockedList + $MockedArray = @($MockedList, $MockedList) Mock -CommandName Get-VMCService -MockWith { $object } diff --git a/Pester/Functions/Get-VmcSddc.tests.ps1 b/Pester/Functions/Get-VmcSddc.tests.ps1 index 6f451aa..7d848a3 100644 --- a/Pester/Functions/Get-VmcSddc.tests.ps1 +++ b/Pester/Functions/Get-VmcSddc.tests.ps1 @@ -9,67 +9,85 @@ Describe "$functionName" -Tag 'Unit' { $display_name = "MockedDisplayName" $user_name = "MockedUserName" - $OrgName = "MockedDisplayName" + $OrgName = "MockedOrgName" $created = "MockedDate" + $OrgId = "Mocked OrgID" + $name = "MockedSDDCName" + $Notname = "NotTheName" $id = "MockedId" - $Service = "com.vmware.vmc.orgs" + $Service = "com.vmware.vmc.orgs.sddcs" $MockedList = [PSCustomObject]@{ "display_name" = $display_name - "name" = $OrgName + "name" = $name + "created" = $created + "user_name" = $user_name + "id" = $id + } + $MockedList2 = [PSCustomObject]@{ + "display_name" = $display_name + "name" = $Notname "created" = $created "user_name" = $user_name "id" = $id } - $object = [PSCustomObject]@{} + $object = @( + @{"Id" = $Id} + ) $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } - $MockedArray = @() - $MockedArray += $MockedList - $MockedArray += $MockedList + $MockedArray = @($MockedList, $MockedList2) Mock -CommandName Get-VMCService -MockWith { $object } + Mock -CommandName Get-VMCOrg { $object } + Context "Sanity checking" { $command = Get-Command -Name $functionName defParam $command 'Name' + defParam $command 'Org' } Context "Behavior testing" { - It "calls get-service to com.vmware.vmc.orgs" { - { Get-VMCOrg -name $OrgName } | Should Not Throw + It "calls Get-VMCOrg" { + { Get-VMCSDDC -Org $OrgId } | Should Not Throw + Assert-MockCalled -CommandName Get-VMCOrg -Times 1 -Scope It + } + It "calls Get-VMCOrg with the SDDC name supplied" { + { Get-VMCSDDC -Org $OrgId -name $name} | Should Not Throw + Assert-MockCalled -CommandName Get-VMCOrg -Times 1 -Scope It -ParameterFilter { $name -eq $name } + } + # Testing with single "Org" so assert call twice. + It "calls get-service to com.vmware.vmc.orgs.sddcs" { + { Get-VMCSDDC -Org $OrgId } | Should Not Throw Assert-MockCalled -CommandName Get-VMCService -Times 1 -Scope It -ParameterFilter { $name -eq $Service } } + + # Testing with two "Orgs" so assert call twice. + It "calls get-service to com.vmware.vmc.orgs.sddcs" { + $object = @( + @{"Id" = 1} + @{"Id" = 2} + ) + $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray } + { Get-VMCSDDC -Org $OrgId } | Should Not Throw + Assert-MockCalled -CommandName Get-VMCService -Times 2 -Scope It -ParameterFilter { $name -eq $Service } + } + # Testing a single SDDC response - It "gets the orgs via list method and returns the properties" { + It "gets the SDDC details via list method and returns the properties" { $object = [PSCustomObject]@{} $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } - $(Get-VMCOrg -name $OrgName).display_name | Should -be $display_name - $(Get-VMCOrg -name $OrgName).name | Should -be $OrgName - $(Get-VMCOrg -name $OrgName).user_name | Should -be $user_name - $(Get-VMCOrg -name $OrgName).created | Should -be $created - $(Get-VMCOrg -name $OrgName).id | Should -be $id + $(Get-VMCSDDC -Org $OrgId).display_name | Should -be $display_name } # Testing the multiple SDDC response - It "calls the Connect-CisServer" { - $object = [PSCustomObject]@{} + It "gets the SDDC details of the SDDC supplied and returns the properties" { + $object = @{} $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray } - { Get-VMCOrg -name $OrgName } | Should Not Throw - - $(Get-VMCOrg -name $OrgName)[0].display_name | Should -be $display_name - $(Get-VMCOrg -name $OrgName)[0].name | Should -be $OrgName - $(Get-VMCOrg -name $OrgName)[0].user_name | Should -be $user_name - $(Get-VMCOrg -name $OrgName)[0].created | Should -be $created - $(Get-VMCOrg -name $OrgName)[0].id | Should -be $id - - $(Get-VMCOrg -name $OrgName)[1].display_name | Should -be $display_name - $(Get-VMCOrg -name $OrgName)[1].name | Should -be $OrgName - $(Get-VMCOrg -name $OrgName)[1].user_name | Should -be $user_name - $(Get-VMCOrg -name $OrgName)[1].created | Should -be $created - $(Get-VMCOrg -name $OrgName)[1].id | Should -be $id + $(Get-VMCSDDC -Org $OrgId -name $name).name | Should -be $name } } }