Comments (3)
Hi. Thanks for the detailed report with repros!
This isn't an Pester-issue, but expected PowerShell's behavior when using a cmdlet/advanced function which you enable by using [CmdletBinding()]
or [Parameter()]
.
The simple solution is to remove your param-block in the mock scriptblock.
The param-block is not necessary as mocks are generated with all the original parameters and parameter-types.
When calling a mock, you actually invoke a Pester-generated wrapper which accepts the same [string[]]$Path
parameter as Test-Path
. After some internal logic, the parameter is passed to the scriptblock you provide, which tries to convert it to [string]
.
I'm not sure why the PowerShell codebase allows the [string[] -> [string]
conversion for simple functions, but in general advanced functions/cmdlets are a lot more strict when it comes to parameter validations.
# Using [string[]] as input as that's what the Pester will pass to
# your mock's scriptblock unless you use `Mock -RemoveParameterType 'Path' ...` which changes it to `[object]`
$myValue = [string[]]('SomePath')
function test {
param(
[Parameter()]
[string]$Path
)
if ($Path -eq 'SomePath') {
return $true
} else {
return $false
}
}
test -Path $myValue
# output
test: C:\Users\Frode\Desktop\Demo\demo.ps1:16:12
Line |
16 | test -Path $myValue
| ~~~~~~~~
| Cannot process argument transformation on parameter 'Path'. Cannot convert value to type System.String.
Wrapping the
Test-Path
expression in curly braces corrected this for that case:It "Tests the top-level folder. Relative path." { { Test-Path 'SomeHostFolder' } | Should -Be $true }
However, a test which generated a
$false
response from the mock would now result in this output:
Expected $false, but got { Test-Path 'Invalid' }.
The first sample is a false positive and intended behavior. Should -Be
uses the -eq
operator like $ExpectedValue -eq $ActualValue
. The provided scriptblock isn't executed but passed as-is as the -ActualValue
parameter in Should
. Because it's used on the right side of the operator, it will be cast to the type of your expected value, $true
(bool), on the left side. A scriptblock is not null, which usually becomes $true
when cast to [bool]
.
> $true -eq { 'hello' }
True
from pester.
Thanks so much for your detailed response, @fflaten! This is my first time using PowerShell and Pester, so I hope you'll forgive my lack of knowledge.
Thanks also for the explanation of the string[] -> string coercion and the scriptblock evaluation to $true. I'm sure that will be useful on future projects...if I ever get this one finished :)
from pester.
You're very welcome. I'm sure this issue will be useful for others in the future as well, so time well spent.
Good luck with your project 🙂
from pester.
Related Issues (20)
- Mock Azure cli command HOT 5
- Use Pester to mock kubectl command
- pester.dev site does not load properly HOT 2
- Mocking a function with OrderedDictionary parameter throws error with PowerShell 7, runs fine with PowerShell 5 HOT 1
- Should -ActualValue $result.TraceFlag -BeOfType 'System.UInt32[]' regression.
- `$SecureString |Should -be` testable HOT 3
- Errors from tests not displaying variables HOT 1
- ScriptBlocks as advanced alternative to name templating for data-driven tests HOT 4
- Test execution sometimes throws a NullReferenceException error HOT 7
- Pester throws an exception when run inside runspace HOT 11
- Missing tests for custom assertions
- Mocks of functions in nested modules fail on Invoke-Pester wildcard discovery HOT 3
- Should -HaveParameter -DefaultValue does not work for boolean types or integer value of 0 (regression) HOT 2
- Pester module warning 'CN=Jakub Jareš, O=Jakub Jareš, L=Praha, C=CZ' HOT 15
- Pester CI Pipeline Should Halt on 'BeforeDiscovery' Failure HOT 3
- Failure in Discovery when using different parameters across files HOT 3
- Select-Object and New-MockObject - Discarded PSObject HOT 2
- Mock CommandNotFoundException: Could not find Command Invoke-Sqlcmd HOT 4
- Bring back 'InconclusiveCount' property (or alternative) on returned Pester.Run object HOT 19
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pester.