336 questions
0
votes
1
answer
90
views
Why Pester requires InModuleScope for unit tests to run successfully? Why mocks won't work as expected with dot sourcing?
I'm exploring Pester for unit testing my PowerShell scripts, and I'm running into a mocking issue.
I dot-source my functions like this:
. "$PSScriptRoot\tz-docker.functions.ps1"
But my ...
4
votes
1
answer
117
views
Create an instance in PowerShell of a .NET type without a constructor
I have a PowerShell script for setting up a website in IIS. It includes the following function:
function Set-IisWebsiteBinding (
[Parameter(Mandatory = $true)]
[string]$SiteName,
[...
1
vote
1
answer
154
views
Trouble mocking Powershell function with Pester that takes a Pipeline parameter
I am receiving the following error when trying get this pester test to work for a function that receives a pipeline parameter.
"An error occured running Pester: The input object cannot be bound ...
1
vote
1
answer
27
views
Cannot get Pester Test to use Mock for "New-Object OfficeOpenXml.ExcelPackage"
I have been unable to get the mock for New-Object to work.
Below is the function and the test inline.
Import-Module ImportExcel -Force
function Clear-ExcelWorksheet {
[CmdletBinding()]
param (...
0
votes
0
answers
106
views
Failure in VS Code Pester extension when refreshing the tests
I am using VS Code for my PowerShell development. I have version 5.1 of PowerShell and version 1.95.3 of VS Code. I have the Pester extension installed in VS Code (version v2023.7.7). Until recently, ...
0
votes
1
answer
185
views
How to mock New-AzStorageContext in Pester
I would like to mock New-AzStorageContext to return a value that can be used as the DestContext parameter of the function Start-AzStorageBlobCopy.
I have tried New-MockObject of Microsoft.WindowsAzure....
0
votes
1
answer
98
views
Pester 'It' or 'Context' array iteration
Pester 5.6.1
Powershell 7.4.5
Here's the deal. I have data that I am loading in the BeforeDiscovery. I can reference all the variables within at the Run stage...no problem there. Even the string ...
0
votes
1
answer
64
views
How to test function that mocks Get-ChildItem w/ -File parameter?
This Pester test:
BeforeAll {
function Get-HtmlFileDetail {
param (
[string[]]$Path = 'E:\Domains',
[string[]]$Include = 'Alive*.html'
)
return Get-...
1
vote
1
answer
156
views
How to use Pester New-MockObject with class from within a module
Synopsis: When trying to calling New-MockObject -Type 'Config' where Config is a class defined in another psm1 file, I get the following error PSInvalidCastException: Cannot convert the "Config&...
2
votes
1
answer
282
views
PowerShell Pester : Use a variable in multiple It-blocks and recognize function when invoked from PowerShell Window
I have the following Pester Test-script:
function Get-Name{
return "TestObject"
}
Describe{
BeforeAll{
$Script:ObjectID
$API::[API]::New()
}
It 'Get ...
3
votes
1
answer
375
views
Comparing hashtables in Pester
What's the correct way to compare hashtables in Pester tests?
i.e. the following gives the error: InvalidResult: Expected System.Collections.Hashtable, but got System.Collections.Hashtable.
$expected =...
0
votes
0
answers
92
views
Passing Context ForEach variables to It blocks
Why are the variables in my Context -ForEach cases not being passed to the It blocks underneath the Context? I have this code in my test file.
BeforeAll {
Import-Module Pester
Import-Module ...
0
votes
2
answers
182
views
Pester Test Case giving error in BeforeDiscovery with null value
Whenever I am trying to use parameter value in BeforeDiscovery under Context in Pester test case it's giving below error. Please help me.
Error::
##[error] [-] Discovery in D:\a\1\s\deploymenttest\...
0
votes
0
answers
110
views
Minimize Boilerplate Code in PowerShell Module Pester Test files
I am building a PowerShell module using the PSModuleDevelopment's MiniModule template generator. It makes a project that looks like this:
PROJECT_ROOT/
├─ the_module_name/
│ ├─ internal/
│ │ ├─ ...
3
votes
1
answer
549
views
Pester Mock a command within a PowerShell class function
I've found a lot of questions about mocking a function (method) within a PowerShell class. The question I have (and can't find an answer for) is, is it possible to mock a cmdlet call that is contained ...
1
vote
1
answer
102
views
How to get Pester Mocks working in a script
I not able to get Pester 5.5.0 Mocks working. I have defined a Mock for Get-Date in the Describe block but when the tests run it does not appear to be invoked.
MyFunction.ps1
function Get-CurrentDate {...
1
vote
1
answer
461
views
Writing pester test for existing script [closed]
I've started reading up on the Pester framework and I could use some thinking power on how to handle my situation. We have several existing PowerShell scripts.
Simple example MyScript.ps1 (pseudo code)...
1
vote
1
answer
126
views
Can Pester v5 Mock command be called outside an It, Before or After block?
I have some old Pester v4 tests that are failing when I try to run them in Pester v5.
With Pester v4 I read that once Mock is called it will continue to apply for the rest of the Describe block, even ...
3
votes
2
answers
437
views
PowerShell module with class defined in separate file fails Pester tests in GitHub Actions
I am creating a PowerShell module that defines a class. For example:
class MyClass
{
[string] $Name
}
If I put the class definition directly in the psm1 file then everything works fine. However, ...
0
votes
0
answers
364
views
Run a test in PowerShell with $PSScriptRoot raises an error
I want to create a test with Pester. I have to create some files and then read them with a filter.
The problem I get is when I use $PSScriptRoot. If I run the test in the Windows PowerShell ISE, it is ...
0
votes
1
answer
277
views
Validate a list of files in PowerShell with Pester
I want to create a PowerShell script that can delete a bunch of file in a given paths following a particular match (in the following example all the files that contain TMP). This is the function:
...
0
votes
1
answer
124
views
Testing a function with Pester got a wrong result
still trying to learn Pester, I got a snippet from ChatGPT:
function Get-EvenNumbers {
param(
[Parameter(Mandatory=$true)]
[int]$start,
[Parameter(Mandatory=$true)]
[int]$end
)
$evenNumbers = @()
...
0
votes
1
answer
105
views
Pester - Execute the same set of tests for different modules
I currently have a PowerShell module that extends another module. Basically, my new module re-implements one Function to provide custom behavior for a specific customer:
Base Module - Implements 12 ...
0
votes
1
answer
477
views
How do I check if a specific exception type has been thrown in Pester 5.4.0?
I want to write a test in PowerShell with Pester 5.4.0 to check whether a specific exception type has been thrown inside a script block. It should be fairly easy, but I am somehow not doing it right. ...
3
votes
2
answers
1k
views
Is there any way to test functions in a PowerShell script without executing the script?
I would like to define stand-alone functions in my PowerShell script and be able to Pester test the functions without executing the rest of the script. Is there any way to do this without defining the ...