# Error on Azure Storage Tables using Peter Test Framework by adding PowerShell commands

**URL:** <https://forums.powershell.org/t/error-on-azure-storage-tables-using-peter-test-framework-by-adding-powershell-commands/24769>\
**Category:** Pester\
**Created:** [September 19, 2024, 3:35pm UTC](https://forums.powershell.org/t/error-on-azure-storage-tables-using-peter-test-framework-by-adding-powershell-commands/24769 "2024-09-19T15:35:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sreekanchu](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@sreekanchu](https://forums.powershell.org/u/sreekanchu)\
**Post date:** [September 19, 2024, 3:35pm UTC](https://forums.powershell.org/t/error-on-azure-storage-tables-using-peter-test-framework-by-adding-powershell-commands/24769/1 "2024-09-19T15:35:35Z")

</div>

I’m getting error while trying to list the azure storage tables using Pester framework. The same commands in PowerShell are working fine. My requirement is I want to convert the PowerShell commands as a test in Pester Test Framework. I am connecting to Azure using Service Principal. The commands are:

```auto
$appId = "<app/client Id>"

$tenantId = "<tenant id>"

$clientSecret = "<client secret>"

$secureClientSecret = ConvertTo-SecureString -String $clientSecret -AsPlainText -Force

$psCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $appId, $secureClientSecret

Connect-AzAccount -ServicePrincipal -Credential $pscredential -Tenant $tenantId

$resourceGroupName = "dev-integrationbus"

$storageAccountName = "devisstorage"

$tableName = "InterfaceLogs"

$context = New-AzStorageContext -StorageAccountName $storageAccountName

$storageTable = Get-AzStorageTable -Name $tableName -Context $context

Get-AzStorageTable -Context $context | select Name
 

```

My Pester test script code is:

```auto
# Import Pester and Az Storage modules
Import-Module Pester
Import-Module Az.Storage

# Define the Pester test script
Describe "Azure Storage Account Operations Test" {

    # Mock the sensitive values to avoid real authentication
    BeforeAll {
        $appId = "<app/client Id>"
        $tenantId = "<tenant id>"
        $clientSecret = "<client secret>"  

    # Mock value for client secret in tests
        $secureClientSecret = ConvertTo-SecureString -String $clientSecret -AsPlainText -Force
        $psCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $appId, $secureClientSecret
    }

    # Mock Azure cmdlets in the correct scope
    BeforeEach {
        # Mock Connect-AzAccount command
        Mock -CommandName Connect-AzAccount {
            Write-Host "Mocked Connect-AzAccount called"
        }

        # Mock New-AzStorageContext to return a more realistic mock for IStorageContext
        Mock New-AzStorageContext {
            $mockContext = New-Object -TypeName PSObject
            $mockContext | Add-Member -MemberType NoteProperty -Name "StorageAccountName" -Value "mockStorageAccount"
            $mockContext | Add-Member -MemberType NoteProperty -Name "ContextType" -Value "Storage"
            return $mockContext
        }

        # Mock Get-AzStorageTable command
        Mock -CommandName Get-AzStorageTable {
            return [pscustomobject]@{
                Name = "InterfaceLogs"
            }
        }
    }

    # Test if Connect-AzAccount runs successfully
    It "Should connect to Azure with the provided service principal" {
        Connect-AzAccount -ServicePrincipal -Credential $psCredential -Tenant $tenantId

        # Assert the Connect-AzAccount command was called
        Assert-MockCalled Connect-AzAccount -Exactly 1 -Scope It
    }

    # Test storage table context creation and table retrieval
    It "Should retrieve the Azure Storage Table" {
        # Act
        $context = New-AzStorageContext -StorageAccountName "devisstorage"
        $table = Get-AzStorageTable -Context $context

        # Assert
        $table | Should -Not -BeNullOrEmpty
        $table.Name | Should -Be "InterfaceLogs"

        # Verify if the mocks were called correctly
        Assert-MockCalled New-AzStorageContext -Exactly 1 -Scope It
        Assert-MockCalled Get-AzStorageTable -Exactly 1 -Scope It
    }
}

```

By running: _Invoke-Pester -Path .\I010.Tests.ps1 -OutputFormat NUnitXml -OutputFile C:\Users\srikant\Desktop\PesterReport\TestResults.xml_

The error it shows is:

Starting discovery in 1 files. Discovery found 2 tests in 54ms. Running tests. Mocked Connect-AzAccount called [-] Azure Storage Account Operations Test.Should retrieve the Azure Storage Table 68ms (64ms|5ms) PSInvalidCastException: Cannot convert the “@{StorageAccountName=mockStorageAccount; ContextType=Storage}” value of type “System.Management.Automation.PSCustomObject” to type “Microsoft.Azure.Commands.Common.Authentication.Abstractions.IStorageContext”. ArgumentTransformationMetadataException: Cannot convert the “@{StorageAccountName=mockStorageAccount; ContextType=Storage}” value of type “System.Management.Automation.PSCustomObject” to type “Microsoft.Azure.Commands.Common.Authentication.Abstractions.IStorageContext”. ParameterBindingArgumentTransformationException: Cannot process argument transformation on parameter ‘Context’. Cannot convert the “@{StorageAccountName=mock StorageAccount; ContextType=Storage}” value of type “System.Management.Automation.PSCustomObject” to type “Microsoft.Azure.Commands.Common.Authentication.Abstractions.IStorageContext”. at , C:\Users\srikant\Desktop\Automation\_Sept13\_Clone\tst\testProjects\BasicTests\folder1\I010.Tests.ps1:52 Tests completed in 286ms Tests Passed: 1, Failed: 1, Skipped: 0, Inconclusive: 0, NotRun: 0

Here, I am looking to try the same PowerShell commands given above using Pester Framework as a test script and fetch the list of azure storage tables within Azure Storage Account.

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [September 19, 2024, 8:54pm UTC](https://forums.powershell.org/t/error-on-azure-storage-tables-using-peter-test-framework-by-adding-powershell-commands/24769/2 "2024-09-19T20:54:26Z")

</div>

Hi, welcome to the forum 👋

Looking for posts with [similar errors](https://stackoverflow.com/questions/36041017/pester-mock-error-psinvalidcastexception-on-azure-cmdlet-call), the problem appears to be that `Get-AzStorageTable` is expecting an object of type `IStorageContext` but when you mock `New-AzStorageContext` you’re returning a `PSObject`.

Try `New-MockObject` (not tested).

```PowerShell
Mock New-AzStorageContext {
    $MockObjectParam = @{
        Type = 'Microsoft.Azure.Commands.Common.Authentication.Abstractions.IStorageContext'
        Properties = @{
            StorageAccountName = 'mockStorageAccount'
            ContextType = 'Storage'
        }
    }    
    $MockContext = New-MockObject @MockObjectParam
    return $MockContext
}

```

By the way, while it’s great you formatted your code correctly in your first post (well done!), errors can also be wrapped in backticks which really helps with readability.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [October 19, 2024, 8:54pm UTC](https://forums.powershell.org/t/error-on-azure-storage-tables-using-peter-test-framework-by-adding-powershell-commands/24769/3 "2024-10-19T20:54:46Z")

</div>

Closed on October 19, 2024, 8:54pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
