Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Splatting parameters in ParameterFilter of Assert-MockCalled #862

Closed
DarkLite1 opened this issue Sep 5, 2017 · 5 comments
Closed

Splatting parameters in ParameterFilter of Assert-MockCalled #862

DarkLite1 opened this issue Sep 5, 2017 · 5 comments

Comments

@DarkLite1
Copy link
Contributor

DarkLite1 commented Sep 5, 2017

I was wondering if it's possible to use a hashtable within the ParameterFilter of Assert-MockCalled? An example might clear things up:

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
. "$here\$sut"

$DoStuffParams = @{
    Name = 1
    Type = 2
}

Function DoStuff {
    Param (
        $Name,
        $Type
    )
    Write-Host "Do stuff"
}

Describe "DoStuff" {
    Mock DoStuff 
        
    it 'Good' {
        DoStuff -Name 1 -Type 2
        DoStuff -Name 2
        Assert-MockCalled DoStuff -Exactly 1 -Scope it -ParameterFilter {$Name -eq 1 -and $Type -eq 2}
    }
    it 'Good' {
        DoStuff @DoStuffParams
        DoStuff -Name 2
        Assert-MockCalled DoStuff -Exactly 1 -Scope it -ParameterFilter {$Name -eq 1 -and $Type -eq 2}
    }
    it 'Bad' {
        DoStuff @DoStuffParams
        DoStuff -Name 2
        Assert-MockCalled DoStuff -Exactly 1 -Scope it -ParameterFilter {$DoStuffParams}
    }
}

It would really be great to be able to use Assert-MockCalled DoStuff -Exactly 1 -Scope it -ParameterFilter {@DoStuffParams}, because the hashtable $DoStuffParams is filled in the production script with data/parameters.

Thank you for your help.

@DarkLite1 DarkLite1 changed the title Splatting paramters in ParameterFilter of Assert-MockCalled Splatting parameters in ParameterFilter of Assert-MockCalled Sep 5, 2017
@it-praktyk
Copy link
Contributor

The related issue #796, #88.

@davebrannan, what about your results?

@it-praktyk
Copy link
Contributor

I assume that the issue can be closed now. Feel free to reopen if you need more assistance.

@DarkLite1
Copy link
Contributor Author

I would like to have it reopened as there isn't a valid workaround to use a HashTable within the -ParameterFiler.

@it-praktyk it-praktyk reopened this Sep 8, 2017
@nohwnd nohwnd added this to the v4.1 milestone Sep 8, 2017
@it-praktyk it-praktyk removed this from the v4.1 milestone Nov 11, 2017
@nohwnd nohwnd added this to the Better Mock milestone Dec 16, 2018
@nohwnd
Copy link
Member

nohwnd commented Dec 16, 2018

This might be complimentary #1158 and #1162 where we could explicitly provide context to the scriptblock . This would possibly simplify testing among scopes, and would not require the user to understand in which scope the scriptblock is invoking, because then we would simply invoke it in the most logical scope, and not in the most practical scope (eg. Assert-MockCalled -ModuleName A -ParameterFilter { $name -eq "Jakub"} would execute the parameter filter in the module A scope and not in the test script scope (Caller scope). There is Invoke-WithContext in assert that I recently implemented that should be easy to leverage for this kind of thing. It's a more advanced work, and some discussion will be needed but it should be doable.

@nohwnd
Copy link
Member

nohwnd commented May 21, 2021

Most of the problems of moving parameters to the right scope should be solved by running the parameter filter in the test scope which is how it is done now. Since this was not requested by anyone else, and it seems to complicate the already complicated situation around mocking I am closing this as won't fix.

@nohwnd nohwnd closed this as completed May 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants