Author Posts

August 23, 2017 at 9:32 pm

Hi,

For unknown reasons when I attempt to test this function my terminal calls "Send-myEmail" and waits for user input to fill in parameters. I find this odd because I have it within the Mocks section of my pester test. Its as if running the Pester test is not mocking the "Send-myEmail" command. I have tried many different variations and cannot get this to work.

FYI – "Send-myEmail" takes in all of the regular parameters "Send-Message" would but I made my own function and defaulted many of the parameters for easier use.

Please help!

function Search-RabbitSubString {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true,
            ValueFromPipeline = $true)]
        [string]$Substring
    )
    $expected_pattern = 'partitions":\[\]'
    Write-Verbose "Searching for $expected_pattern"
    $result = [bool] (Select-String -InputObject $substring -Pattern $expected_pattern)

    if ($result) {
        "Expected pattern in substring was found" | Add-ToRabbitLog
        return $true
    }
    Write-Warning "Partition check failed!"
    "Some message" | Add-ToRabbitLog
    Send-MyEmail -subject "something" -body "something"
}
Context 'Testing functionality' {
            mock Write-Warning -MockWith { return "Warning"}
            mock Add-ToRabbitLog -MockWith {}
            mock Send-MyEmail -mockwith { return "Success" }
            Search-RabbitSubString -Substring "fail"
            it 'should fail and send myemail' {
                Assert-MockCalled -CommandName Write-Warning -Times 1
                Assert-MockCalled -CommandName Add-ToRabbitLog -Times 1
                Assert-MockCalled -commandName Send-MyEmail -Times 1
            }

August 24, 2017 at 11:07 pm

When you mock a command in Pester it doesn't completely nullify it. It creates a proxy command which copies all of it's parameters. I bet that your Send-MyEmail function has a mandatory parameter that's a credential.