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

Pass on values from the pipeline to Assert-Equivalent #51

Closed
ronaldborman opened this issue Jun 15, 2024 · 8 comments · Fixed by #52
Closed

Pass on values from the pipeline to Assert-Equivalent #51

ronaldborman opened this issue Jun 15, 2024 · 8 comments · Fixed by #52

Comments

@ronaldborman
Copy link
Contributor

ronaldborman commented Jun 15, 2024

Most assertions seem to accept values from the pipeline (based on the examples in the Readme.md of this project).

Using Pester 5.5 and Assert 0.9.6 I tried to change the Assert-Equivalent example (which instead uses a parameter for the actual value) so that the actual value is passed from the pipeline:

Describe 'Assert-Equivalent' {
  It 'should use the input from the pipeline' {
    $expected = [PSCustomObject]@{
      Name      = 'Jakub'
      Age       = 28
      Languages = 'Czech', 'English'
    }

    $actual = [PSCustomObject]@{
      Name      = 'Jkb'
      Languages = 'Czech', 'English', 'German'
    }

    #Assert-Equivalent -Actual $actual -Expected $expected
    $actual | Assert-Equivalent -Expected $expected
  }
}

But instead of the differences in the expected and actual objects I get the following error:

Assert-Equivalent : The input object cannot be bound to any parameters for the command either because
the command does not take pipeline input or the input and its properties do not match any of the par
ameters that take pipeline input.

This also happens when I additionally pass the expected value as a positional parameter:
$actual | Assert-Equivalent $expected

As the example passes the expected value as a named parameter, it suggests it is not possible to pass the actual value from the pipeline. If so, would this be a feature you're willing to consider (preferably with a positional parameter as the expected value)?

@nohwnd
Copy link
Owner

nohwnd commented Jun 15, 2024 via email

@ronaldborman
Copy link
Contributor Author

Although I'm more of an occasional user of PowerShell and don't know all the ins and outs (this would actually be my first PowerShell related PR) I will give it a try....

Looking at the source code of this repository and the should-beequivalent assertion in the Pester repository, will it be enough to change the parameter declaration of the Assert-Equivalent function to the following?

    [CmdletBinding()]
    param(
        [Parameter(Position = 1, ValueFromPipeline = $true)]
        $Actual,
        [Parameter(Position = 0, Mandatory)]
        $Expected,
        $Options = (Get-EquivalencyOption),
        [Switch] $StrictOrder
   )

And then add a couple of tests to Assert-Equivalent.Tests.ps1?

@nohwnd
Copy link
Owner

nohwnd commented Jun 15, 2024 via email

@ronaldborman
Copy link
Contributor Author

I've made the PR, but the AppVeyor build seems to have failed. Not sure if I did something wrong.

@nohwnd
Copy link
Owner

nohwnd commented Jun 18, 2024

You did not, the build upgraded to Pester 6-alpha1 where I used the same names for assertions and so the tests were testing that rather than the assertions coming from Assert.

@ronaldborman
Copy link
Contributor Author

PR merged, I will close this issue. Thanks for the learning opportunity.

@nohwnd
Copy link
Owner

nohwnd commented Jun 20, 2024

Thanks for you contribution :)

@nohwnd
Copy link
Owner

nohwnd commented Jun 20, 2024

Your further contributions would be welcome on Pester repo :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants