简体   繁体   中英

Using 'Pester' (PowerShell Unit-Testing framework) with properties

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)
    }
}

这似乎是他们在第 2 版中调查的一个限制。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM