Search code examples
powershellpester

Test a collection for equality or equivalence in Pester


In nUnit, we can do something like this:

Expect(actualColleciton, EquivalentTo(expectedCollection));

and

Expect(actualCollection, EqualTo(expectedCollection));

Is there an equivalent in Pester?

I know I can do

$actualCollection | Should Be $expectedCollection

but it does NOT behave as you would expect.

Am I using the right syntax?


Solution

  • I am guessing that you want to compare the contents of your collection, not the pointer/address to the collection.

    I think you could inspire from something like:

    $a1=@(1,2,3,4,5)
    $b1=@(1,2,3,4,5,6)
    $ret = (Compare-Object $a1 $b1).InputObject
    
    if ($ret)
    {
    "different"
    }
    else
    {
    "same"
    }
    

    to do something like:

    $ret = (Compare-Object $actualCollection $expectedCollection).InputObject
    $ret | Should Be $null 
    

    where $null indicates the lists are the same.