Wraped all the tests inModuleScope for invoke-pester invocation.

Moved function name into module scope.
This commit is contained in:
Conor Tolan
2019-02-23 18:56:44 +00:00
parent b90c83ac56
commit 5c833d4ce5
5 changed files with 264 additions and 252 deletions

View File

@@ -1,78 +1,80 @@
#Requires -Modules Pester, VMware.VMC
$functionName = $MyInvocation.MyCommand.Name.TrimEnd(".Tests.ps1")
Import-Module -Name VMware.VimAutomation.Cis.Core
Describe "$functionName" -Tag 'Unit' {
. "$PSScriptRoot/Shared.ps1"
inModuleScope VMware.VMC {
$functionName = "Get-VMCTask"
Describe "$functionName" -Tag 'Unit' {
. "$PSScriptRoot/Shared.ps1"
$global:DefaultVMCServers = $true
$global:DefaultVMCServers = $true
$OrgId = "Mocked OrgID"
$name = "MockedSDDCName"
$Notname = "NotTheName"
$id = "MockedId"
$Service = "com.vmware.vmc.orgs.tasks"
$OrgId = "Mocked OrgID"
$name = "MockedSDDCName"
$Notname = "NotTheName"
$id = "MockedId"
$Service = "com.vmware.vmc.orgs.tasks"
$MockedList = [PSCustomObject]@{
"name" = $name
}
$MockedList2 = [PSCustomObject]@{
"name" = $Notname
}
$object = @(
@{"Id" = $Id}
)
$object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $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 'Org'
}
Context "Behavior testing" {
It "calls Get-VMCOrg with the Org name supplied" {
{ Get-VMCTask -Org $name} | Should Not Throw
Assert-MockCalled -CommandName Get-VMCOrg -Times 1 -Scope It -ParameterFilter { $name -eq $name }
$MockedList = [PSCustomObject]@{
"name" = $name
}
$MockedList2 = [PSCustomObject]@{
"name" = $Notname
}
# Testing with single "Org" so assert call twice.
It "calls get-service to com.vmware.vmc.orgs.tasks" {
{ Get-VMCTask -Org $OrgId } | Should Not Throw
Assert-MockCalled -CommandName Get-VMCService -Times 1 -Scope It -ParameterFilter { $name -eq $Service }
$object = @(
@{"Id" = $Id}
)
$object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $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 'Org'
}
# Testing with two "Orgs" so assert call twice.
It "calls get-service to com.vmware.vmc.orgs.tasks" {
$object = @(
@{"Id" = 1}
@{"Id" = 2}
)
$object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray }
{ Get-VMCTask -Org $OrgId } | Should Not Throw
Assert-MockCalled -CommandName Get-VMCService -Times 2 -Scope It -ParameterFilter { $name -eq $Service }
}
Context "Behavior testing" {
# Testing a single SDDC response
It "gets the task details via list method and returns the properties" {
$object = [PSCustomObject]@{}
$object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList }
$(Get-VMCTask -Org $OrgId).name | Should -be $name
}
# Testing the multiple SDDC response
It "gets the task details of the SDDC supplied and returns the properties" {
$object = @{}
$object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray }
$(Get-VMCTask -Org $OrgId)[0].name | Should -be $name
$(Get-VMCTask -Org $OrgId)[1].name | Should -be $Notname
It "calls Get-VMCOrg with the Org name supplied" {
{ Get-VMCTask -Org $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.tasks" {
{ Get-VMCTask -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.tasks" {
$object = @(
@{"Id" = 1}
@{"Id" = 2}
)
$object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray }
{ Get-VMCTask -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 task details via list method and returns the properties" {
$object = [PSCustomObject]@{}
$object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList }
$(Get-VMCTask -Org $OrgId).name | Should -be $name
}
# Testing the multiple SDDC response
It "gets the task details of the SDDC supplied and returns the properties" {
$object = @{}
$object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray }
$(Get-VMCTask -Org $OrgId)[0].name | Should -be $name
$(Get-VMCTask -Org $OrgId)[1].name | Should -be $Notname
}
}
}
}
}