Dla przykładu mamy funkcję do testowania, która dodaje metody do kastowania na int, double oraz byte
function Add-CastingFunctions($value) {
return Add-Member -InputObject $value -Name ToInt `
-MemberType ScriptMethod -PassThru -Force -Value `
{ [int] $this } |
Add-Member -Name ToDouble `
-MemberType ScriptMethod -PassThru -Force -Value `
{ [double] $this } |
Add-Member -Name ToByte `
-MemberType ScriptMethod -PassThru -Force -Value `
{ [byte] $this }
}
Poniżej mamy test ( w osobnym pliku):Describe "when added cast function to string '5'" {
$patched = Add-CastingFunctions("5")
Context "and I do not attempt to cast it" {
It "returns a string" {
$patched.GetType().Name| Should be "string"
}
}
Context "and I attempt to cast it as an integer" {
It "returns a value that is of type integer" {
$patched.ToInt().GetType().Name| Should be ("Int32")
}
}
Context "and I attempt to cast it as a double" {
It "returns a value that is a double" {
$patched.ToDouble().GetType().Name| Should be ("Double")
}
}
Context "and I attempt to cast it as a byte" {
It "returns a value that is a byte" {
$patched.ToByte().GetType().Name| Should be ("Byte")
}
}
}
W tym samym pliku co jest test, musimy dodać nagłówek:
$pwd = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests", "")
. "$pwd\$sut"
Niestety, trzeba w każdym pliku dodawać ten sam nagłówek, więc wpadłem na pomysł, aby dodać ten nagłówek do snippeta. New-IseSnippet -Title 'pester-header' -Description 'Adds pester header' -Text '
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")
. "$here\$sut"
' -Force
Teraz, dzięki temu snippetowi możesz w PS_ISE dodać nagłówek za pomocą CTRL + J
Brak komentarzy:
Prześlij komentarz