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)
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
msgisme
  • 193
  • 1
  • 9
  • 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." https://github.com/pester/Pester/issues/33 – msgisme Nov 30 '12 at 20:02

2 Answers2

1

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
msgisme
  • 193
  • 1
  • 9
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Scott Muc
  • 2,873
  • 27
  • 38
  • 1
    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? – msgisme Feb 22 '13 at 16:00
  • One thing that would help is a page explaining how to setup Pester- I'm curious when I follow the directions here: http://scottmuc.com/blog/development/pester-bdd-for-the-system-administrator/... what version do I get, 1.2 or 2.0? – msgisme Feb 22 '13 at 16:22