2012-11-20 56 views
1

我想圍繞一些擴展帶屬性的對象的PowerShell代碼包裝文本夾具。我得到一個似乎是由於pester引起的錯誤。我在下面有一個人爲的例子,顯示了我正在嘗試做的事情。使用具有屬性的'Pester'(Powershell單元測試框架)

有沒有人成功地在使用pester使用屬性的函數上編寫測試?

的錯誤,我得到:

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.SelectObjectC 
    ommand 

我的功能:

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

我的測試代碼:

$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) 
     } 
    } 
+0

從上糾纏的github上scottmuc: 。「呸,好像對象擴展導致更多的問題,我們正在考慮去掉$ object.should擴展,並使用管道基於斷言的2.0版本應該解決這個問題」 https://github.com/pester/Pester/issues/33 – msgisme

回答

1

Scottmuc給我的糾纏GitHub的答案。這似乎是他們在第2版中正在研究的限制。

1

Pester version 2.0.1已被默默釋放。你必須重寫你的期望是

$prop.TestProperty | Should Be 3 

這也意味着,所有其他的測試將需要遷移到這條管道的形式展望語法。希望這可以解決你的問題,如果沒有,我會盡我所能爲你工作。

+0

我很難開始使用Pester。我是一名對BDD來說很新鮮的DBA。我需要做的大多數測試都是特定於環境(確保環境已配置)或集成多個步驟。維基似乎專注於測試個人功能。你有什麼建議學習如何使用這個工具? – msgisme

+0

有一件事會幫助解釋如何設置Pester的頁面 - 我很好奇,當我按照這裏的方向:http://scottmuc.com/blog/development/pester-bdd-for-the-system-administrator/ ...我得到什麼版本,1.2或2.0? – msgisme

相關問題