Dont get the beforeAll

Hi,

I am missing something, or something is broken, but I can only seem to get my tests to work if I duplicate the params in the beforeAll within the beforeDiscovery.

From the documentation I read it as anything in the beforeAll should be passed to every test within the describe block.

So why do I need to do this?

BeforeAll {
        $r = Get-Content .\DevTest.Parameters.json | ConvertFrom-Json -AsHashtable
        $expectedvNets = $r.virtualNetworks.Values
        $deployedvNets =  (Get-AzVirtualNetwork).Name
        $expectedResourceGroups = $R.resourceGroups.Values
        $deployedResourceGroups =  (get-AzResourceGroup).ResourceGroupName  
}
Describe 'Azure Infrastructure Testing' {
    BeforeDiscovery {
        $r = Get-Content .\DevTest.Parameters.json | ConvertFrom-Json -AsHashtable
        $expectedvNets = $r.virtualNetworks.Values
        $deployedvNets =  (Get-AzVirtualNetwork).Name
        $expectedResourceGroups = $R.resourceGroups.Values
        $deployedResourceGroups =  (get-AzResourceGroup).ResourceGroupName
    
    }
    Context 'Checking Deployed components align with designs' {
        It "Checking resourceGroup exists" -Foreach $expectedResourceGroups {
            $deployedResourceGroups  | Should -Contain $_
        }
        It "Checking Virtual network exists" -Foreach $expectedvNets {
            $deployedvNets  | Should -Contain $_
        }   
    }      
}

If I remove the before discovery, all tests fail.

Where am I going wrong?

Thanks

It wont work like that way with testcases, see

Discovery and Run | Pester

You use BeforeDiscovery to data drive your tests, which is what you are doing for your foreach. However the “It” doesn’t have access to whats defined in BeforeDiscovery and so you need to put the deployed… in the BeforeAll.

Describe 'Azure Infrastructure Testing' {
    BeforeDiscovery {
        $r = Get-Content .\DevTest.Parameters.json | ConvertFrom-Json -AsHashtable
        $expectedvNets = $r.virtualNetworks.Values
        $expectedResourceGroups = $R.resourceGroups.Values
    
    }
    BeforeAll {
        $deployedvNets =  (Get-AzVirtualNetwork).Name
        $deployedResourceGroups =  (get-AzResourceGroup).ResourceGroupName  
   }
    Context 'Checking Deployed components align with designs' {
        It "Checking resourceGroup exists" -Foreach $expectedResourceGroups {
            $deployedResourceGroups  | Should -Contain $_
        }
        It "Checking Virtual network exists" -Foreach $expectedvNets {
            $deployedvNets  | Should -Contain $_
        }   
    }      
}