pester

Mocking class functions with Pester 5 and PowerShell 7

帅比萌擦擦* 提交于 2021-02-19 07:37:42
问题 Does anyone have an example of mocking a dot-sourced class function with Pester 5 and PowerShell 7? Thank you. Edit: example Classes\MyClass.ps1: class MyClass { [void] Run() { Write-Host "Class: Invoking run..." } } MyModule.psm1: # Import classes . '.\Classes\MyClass.ps1' # Instantiate classes $MyClass = [MyClass]::new() # Call class function $MyClass.Run() 回答1: Pester only mocks commands - not classes or their methods. The easiest way to "mock" a PowerShell class for method dispatch

Show content of hashtable when Pester test case fails

让人想犯罪 __ 提交于 2021-02-05 07:55:23
问题 Problem When a Hashtable is used as input for Should , Pester outputs only the typename instead of the content: Describe 'test' { It 'test case' { $ht = @{ foo = 21; bar = 42 } $ht | Should -BeNullOrEmpty } } Output: Expected $null or empty, but got @(System.Collections.Hashtable). Expected output like: Expected $null or empty, but got @{ foo = 21; bar = 42 }. Cause Looking at Pester source, the test input is formatted by private function Format-Nicely , which just casts to String if the

How to make the output in the pipeline appear on the console when running pester tests?

左心房为你撑大大i 提交于 2021-01-28 05:00:21
问题 By default, the output in the pipeline is hidden,But sometimes I really want to know the output at that time. Of course, I knew I could add additional commands, such as write-host or out-default. But does Pester itself have a mechanism to make the output display properly? I checked the help document and didn't find the relevant content, so I came here for help. 回答1: It is possible to write a custom Should wrapper (proxy) using this technique. The wrapper can write the pipeline objects to the

Pester mock method for Powershell 5 class

喜你入骨 提交于 2021-01-27 05:40:46
问题 I am having an issue trying to mock a powershell 5 class method, when executing the test, I get the error " CommandNotFoundException: Could not find Command FunctionToMock". I am trying to unit test the "OutputToOverwrite" method by mocking "FunctionToMock". I think I would have to mock ChocoClass itself first, but I am not sure how to do it. Thanks. Class ChocoClass { [string] OutputToOverwrite() { return $this.FunctionToMock() } [string] FunctionToMock() { return "This text will be replaced

Pester mock method for Powershell 5 class

不羁的心 提交于 2021-01-27 05:40:35
问题 I am having an issue trying to mock a powershell 5 class method, when executing the test, I get the error " CommandNotFoundException: Could not find Command FunctionToMock". I am trying to unit test the "OutputToOverwrite" method by mocking "FunctionToMock". I think I would have to mock ChocoClass itself first, but I am not sure how to do it. Thanks. Class ChocoClass { [string] OutputToOverwrite() { return $this.FunctionToMock() } [string] FunctionToMock() { return "This text will be replaced

How to create a wrapper for an advanced function cmdlet that uses dynamic parameters

╄→尐↘猪︶ㄣ 提交于 2021-01-16 04:18:56
问题 I'm trying to create a wrapper (proxy) for Pester's Should cmdlet. Possible use cases include transparent logging of test input even in case of success and improve the way Pester logs objects of certain types, e. g. hashtable. As Should is an advanced function, forwarding arguments via $args splatting does not work. So I tried to generate a wrapper using System.Management.Automation.ProxyCommand::Create() , as described by this answer: $cmd = Get-Command Should $wrapperSource = [System

How to create a wrapper for an advanced function cmdlet that uses dynamic parameters

ぃ、小莉子 提交于 2021-01-16 04:14:08
问题 I'm trying to create a wrapper (proxy) for Pester's Should cmdlet. Possible use cases include transparent logging of test input even in case of success and improve the way Pester logs objects of certain types, e. g. hashtable. As Should is an advanced function, forwarding arguments via $args splatting does not work. So I tried to generate a wrapper using System.Management.Automation.ProxyCommand::Create() , as described by this answer: $cmd = Get-Command Should $wrapperSource = [System

How to create a wrapper for an advanced function cmdlet that uses dynamic parameters

怎甘沉沦 提交于 2021-01-16 04:12:50
问题 I'm trying to create a wrapper (proxy) for Pester's Should cmdlet. Possible use cases include transparent logging of test input even in case of success and improve the way Pester logs objects of certain types, e. g. hashtable. As Should is an advanced function, forwarding arguments via $args splatting does not work. So I tried to generate a wrapper using System.Management.Automation.ProxyCommand::Create() , as described by this answer: $cmd = Get-Command Should $wrapperSource = [System

How to create a wrapper for an advanced function cmdlet that uses dynamic parameters

。_饼干妹妹 提交于 2021-01-16 04:12:36
问题 I'm trying to create a wrapper (proxy) for Pester's Should cmdlet. Possible use cases include transparent logging of test input even in case of success and improve the way Pester logs objects of certain types, e. g. hashtable. As Should is an advanced function, forwarding arguments via $args splatting does not work. So I tried to generate a wrapper using System.Management.Automation.ProxyCommand::Create() , as described by this answer: $cmd = Get-Command Should $wrapperSource = [System

Testing for mandatory parameters with Pester

女生的网名这么多〃 提交于 2020-08-04 05:05:01
问题 I'm trying to figure out how to have Pester test for parameters that are missing: Find-Waldo.Tests.ps1 $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.' Describe 'Mandatory paramters' { it 'ComputerName' { { $Params = @{ #ComputerName = 'MyPc' ScriptName = 'Test' } . "$here\$sut" @Params } | Should throw } } Find-Waldo.ps1 Param ( [Parameter(Mandatory)] [String]$ComputerName, [String]$ScriptName ) Function