powershellpester-5

Pester doesn't reflect changes in a script


I'm using Pester version:5.3.3 and not quite sure if I do it properly. I have only one file containing the whole logic: Function.Tests.ps1.

BeforeAll{
 import-module .\Function.Tests.ps1 -Force
 Remove-Item -Path "$PSScriptRoot\testresults.xml" -ErrorAction SilentlyContinue
}

Describe "Test CreateNewTable Function" {
Push-Location
Import-Module sqlserver -DisableNameChecking
Pop-Location

$tableName = 'xxx'
$instance = "myServer"
$dbcmd = @"
SELECT [Name] AS TableName 
  FROM [sys].[tables] 
 WHERE [Name] = '$($tableName)'
"@
 $result = Invoke-Sqlcmd -Query $dbcmd `
                      -ServerInstance $instance `
                      -Database 'master' `
                      -SuppressProviderContextWarning
 It "$tableName should exist" {
   ($result.TableName -eq $tableName) | Should -Be $true
 }
}

When I run the test for the first time with the table "xxx" in place it's all fine and I get - as expected - a success message:

Invoke-Pester .\Function.Tests.ps1

But when I change the script putting a wrong table name, say "yyy", I was expecting an error, but nothing happens and I still get a success message. Strangely enough when I change the server name to a wrong name, Pester reflects that change and I get an error, but changing the dbname to the wrong name brings still a success.

-- Edited I've created a separate two files: a function and the test unit. The function:

Function Get-Table
{  
param(
       [string]$tableName,
       [string]$instance,
       [string]$database
)
$dbcmd = @"
SELECT [Name] AS TableName 
  FROM [sys].[tables] 
 WHERE [Name] = '$($tableName)'
"@
return $result = Invoke-Sqlcmd -Query $dbcmd `
                      -ServerInstance $instance `
                      -Database $database `
                      -SuppressProviderContextWarning
}

and the test unit:

BeforeAll{
. $PSScriptRoot/Get-Table.ps1
}
Describe "Test CreateNewTable Function" {
Import-Module sqlserver -DisableNameChecking
$tableName = 'clusters'
$instance = "xxx"
$database = "YYY"
$result = Get-Table -tableName $tableName -instance $instance - 
database $database
# Now run the test   
It "$tableName should exist" {
    ($result.TableName -eq $tableName) | Should -Be $true
    }
}

Both files are in the same directory, calling the test:

Invoke-Pester C:\PesterTests\Function.Tests.ps1

I'll still get the error:

Discovery in C:\PesterTests\Function.Tests.ps1 failed with: System.Management.Automation.CommandNotFoundException: The term 'Get-Table' is not recognized as the name of a cmdlet, function meaning the function file will not be loaded and the function Get-Table is unknown.


Solution

  • the solution is as simple as that: you can't put the logic in the describe-block (as I was doing), as soon as I put the logic in "it" block it works!

    BeforeAll {
    . $PSScriptRoot/Get-Table.ps1
    }
    Describe 'Get-Table' {
    It "$tableName should exist" {
      Push-Location
      Import-Module sqlserver -DisableNameChecking
      Pop-Location
      $tableName = 'cluster'
      $instance = "xxx"
      $database = "YYY"
      $result = Get-Table -tableName $tableName -instance $instance -database 
      $database
      $result.TableName -eq $tableName | Should -Be $true
      }
    }