0

Hey guys having trouble writing pester for this function. The function is down below. Here is a link of the code if you prefer looking on here. https://www.powershellgallery.com/packages/NetScaler/1.1.2/Content/Public%5CConnect-NetScaler.ps1

Describe 'Connect-NetScaler' {
            $Credential = New-Object PSCredential('username', (ConvertTo-SecureString -AsPlainText -Force 'password'))
            Context 'Successful Connection' {
                Mock Add-Member {
                    return @{
                        Endpoint = '10.111.33.3'
                        Scheme   = 'http'
                    }
                 }
                Mock New-Object {
                    return @{
                    Uri='http://10.111.33.3/nitro/v1';
                }
            }
                Mock ConvertTo-Json -ParameterFilter {
                    $InputObject -eq $Credential
                }
                Mock 'Invoke-RestMethod' {
                 #   $Uri | Should -BeExactly "fakename/config/login"
                   # $Credential | Should -BeOfType PSCredential
                    return [PSCustomObject]@{
                        severity = 'NONE'
                    }
                } -Verifiable
                It 'Should be able to connect'{
                    $result= Connect-NetScaler -IPAddress '10.111.33.3' -Credential $Credential
                    $result | Should -Be "Response:`n"
                }

            }
            Context 'UnSuccessful Connection' {
                Mock Add-Member {
                    return @{
                        Endpoint = '10.111.33.3'
                        Scheme   = 'http'
                    }
                 }
                Mock New-Object {
                    return @{
                    Uri='http://10.111.33.3/nitro/v1';
                }
            }
                Mock ConvertTo-Json -ParameterFilter {
                    $InputObject -eq $Credential
                }
                Mock 'Invoke-RestMethod' {
                 #   $Uri | Should -BeExactly "fakename/config/login"
                   # $Credential | Should -BeOfType PSCredential
                    return [PSCustomObject]@{
                        severity = 'ERROR'
                    }
                } -Verifiable
                It 'Should be able to connect'{
                    $result= Connect-NetScaler -IPAddress '10.111.33.3' -Credential $Credential
                    $result | Should -Throw "Error. See response: `n"
                }
            }
        } 

This is the code for the function. I am a beginner to pester so been having trouble figuring this out. Any help would be greatly appreciated!

  • Please show what you have tried so far. Chances for getting an answer are greater if you can show specific code that you are having trouble with. You could start by writing down a list of allowed inputs and what the function is supposed to return for these inputs. Then start to think about how to formulate these as tests. Often you can't test every possible input, so you choose a few representative values, also edge cases like minimum and maximum values. For invalid values the function should propably report errors, these are separate test cases. – zett42 May 15 '22 at 14:15
  • @zett42 added the pester I've been able to write above – Not on The list May 18 '22 at 05:01
  • Thanks for adding your code. So what is the actual propblem you have with your code? From a quick look I see that the `$Credential = ` line should be in a `BeforeAll {}` block because Pester 5+ doesn't run code that is not in `It`, `BeforeAll`, `BeforeEach`, `AfterAll` or `AfterEach`, [during run phase](https://pester-docs.netlify.app/docs/usage/discovery-and-run). – zett42 May 18 '22 at 12:46

0 Answers0