Code Monkey home page Code Monkey logo

Comments (3)

fflaten avatar fflaten commented on May 24, 2024

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.

daverayment avatar daverayment commented on May 24, 2024

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.

fflaten avatar fflaten commented on May 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.