1

I am trying to wrap a text fixture around some PowerShell code that extends an object with a property. I get an error that appears to be caused by Pester. I have a contrived example below that displays what I am trying to do.

Has anyone succeeded in writing tests on functions that use properties with Pester?

The error I get:

Describing Get-PropertyOfItem
Select-Object : Property cannot be processed because property "should" already exists.
At C:\Repos\ClinicientOps\clinicientops\General\Functions\Get-PropertyOfItem.ps1:4 char:11
+     $files | Select-Object *, @{Name = "TestProperty"; Expression = { $dir.Length}} ...
+              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Windows:PSObject) [Select-Object], PSArgumentException
    + FullyQualifiedErrorId : AlreadyExistingUserSpecifiedPropertyNoExpand,Microsoft.PowerShell.Commands.SelectObjectCommand

My function:

function Get-PropertyOfItem {
    $dir = "C:\"
    $files = Get-ChildItem $dir
    $files | Select-Object *, @{Name = "TestProperty"; Expression = { $dir.Length}} -Last 1
}

My test code:

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

Describe "Get-PropertyOfItem" {

    It "does something useful" {

        $prop = Get-PropertyOfItem
        $prop.TestProperty.should.be(3)
    }
}
1
  • From scottmuc on pester's github: "Bah, seems like the object extensions are causing more issues. We're looking at removing the $object.should extension and using pipeline based assertions for version 2.0 which should fix this." github.com/pester/Pester/issues/33 Commented Nov 30, 2012 at 20:02

2 Answers 2

1

It appears to be a limitation they are investigating in version 2.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you add the context - a reference to the GitHub content?
1

Pester version 2.0.1 has been silently released. You'll have to rewrite your expectation to be

$prop.TestProperty | Should Be 3

It also means that all your other tests will need to migrate to this pipeline form Expectation syntax.

2 Comments

I'm having a really hard time getting started with Pester. I am a DBA who is pretty fresh to BDD. Most of the testing I need to do is either environment specific (make sure the environment is configured) or integrating multiple steps. The wiki seems to focus on testing individual functions. Do you have any suggestions for learning how to use this tool?
One thing that would help is a page explaining how to setup Pester- I'm curious when I follow the directions here: scottmuc.com/blog/development/…... what version do I get, 1.2 or 2.0?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.