Open KarstenHabay opened 3 hours ago
Should -Contain
looks for the same object. Yours are different objects even though they have the same properties and values, so the behavior is expected.
What you need is something like Should-BeEquivalant
available in Pester v6.0.0-alpha5 version which compares two different objects by properties. Combine it with Should-BeAny
to find at least one match in the collection.
E.g. $result | Should-Any { $_ | Should-BeEquivalent $resultObject1 }
You can also use Assert-Any
and Assert-Equivalent
from the module Assert with Pester v5
Tried this (after adding Module Assert v0.9.7):
$resultObject1 = [pscustomobject]@{
Property = 'Name'
CompareObject = 'John'
WithObject = 'Jane'
}
$result | Assert-Any { $_ | Assert-Equivalent $resultObject1 }
but it didn't work.
Got message:
Starting discovery in 1 files.
Discovery found 1 tests in 18ms.
Running tests.
[-] Compare-ParlObjectsValue.Happy Path.Should return common properties and their values 20ms (19ms|1ms)
AssertionException: Expected and actual are not equivalent!
Expected:
PSObject{
CompareObject=John;
Property=Name;
WithObject=Jane
}
Actual:
PSObject{
CompareObject=30;
Property=Age;
WithObject=30
}
Summary:
Expected property .Property with value 'Name' to be equivalent to the actual value, but got 'Age'.
Expected property .CompareObject with value 'John' to be equivalent to the actual value, but got '30'.
Expected property .WithObject with value 'Jane' to be equivalent to the actual value, but got '30'.
at Assert-Equivalent, H:\PowerShell\Source\Parl.Utility\output\RequiredModules\Assert\0.9.7\src\Equivalence\Assert-Equivalent.ps1:676
at <ScriptBlock>, H:\PowerShell\Source\Parl.Utility\tests\Unit\Public\Compare-ParlObjectsValue.tests.ps1:67
at Assert-Any, H:\PowerShell\Source\Parl.Utility\output\RequiredModules\Assert\0.9.7\src\Collection\Assert-Any.ps1:12
at <ScriptBlock>, H:\PowerShell\Source\Parl.Utility\tests\Unit\Public\Compare-ParlObjectsValue.tests.ps1:67
Tests completed in 228ms
Tests Passed: 0, Failed: 1, Skipped: 0, Inconclusive: 0, NotRun: 0
I think it's the result of comparing the assertion with the second object. Cause the second object in the collection is the 'Age' object.
Assert-Any is not doing its job 🤔
Yeah, maybe Assert never supported nested assertions. Sorry about that. Can double check tomorrow, unless @nohwnd answers first. 🙂
We're modifying the code slightly while merging it into Pester v6.
I think it would be better to modify Should -Contain in such a way that it does what it is supposed to do, that is, see if an object is inside a collection even if it is not that same object, just like the PowerShell Comparison Operators: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-7.4#containment-operators Just a hint for @nohwnd 😉
Checklist
What is the issue?
I have a function that has two objects as input, which then returns the common properties with their values for comparison. Then, I wrote my first Pester test, and it doesn't see that the object in the collection is the same as the object contained in its collection as shown in the console output:
Expected Behavior
It should find the object in the collection.
Steps To Reproduce
My PowerShell Function:
My Pester test:
Actual console output:
Describe your environment
Windows 11 24H2 Visual Studio Code 1.95.1 PowerShell 7.4.6 Pester 5.6.1 Using Sampler 0.118.1
Possible Solution?
Debug Pester or Debug Me?