ticketmaster / poshspec

Infrastructure Testing DSL running in Pester
MIT License
183 stars 32 forks source link

Method Invocation Tests for AppPool and WebSite and PropertyExpression for AppPool, WebSite, and CimObject resources #52

Closed jgigler closed 7 years ago

jgigler commented 7 years ago

PropertyExpression

PropertyExpression is a new parameter on Get-PoshspecParam that allows for evaluating an expression that may include method calls and filtering. Example:

Describe Website {
  WebSite 'Default Web Site' 'Bindings.Where({$_.Protocol -match "https"}).Count' { Should be '1' }
}

To implement, add a similar statement to the resource in quesetion:

if ($Property -like '*.*' -or $Property -like '*(*' -or $Property -like '*)*') {
  $PSBoundParameters.Remove("Property")
  $PSBoundParameters.Add("PropertyExpression", $Property)
  $params = Get-PoshspecParam -TestName <your test name> -TestExpression $expression @PSBoundParameters
}

This calls Get-PoshspecParam passing in the Property variable via the -PropertyExpression parameter. Get-PoshspecParam then calls a new function named Invoke-PoshspecExpression that combines the TestExpression with the Property expression. Below shows an example of those two values would be and how then would get joined:

$TestExpression = { Get-IISSIte -SiteName "Default Web Site"}
$PropertyExpression = 'Bindings.Where({$_.Protocol -match "https"}).Count'

(Get-IISSIte -SiteName "Default Web Site").Bindings.Where({$_.Protocol -match "https"}).Count

Note: If you are defining the Poshspec test and using double quotes, make sure to escape the $ properly, or just use single quotes. Otherwise there will be problems with interpolation.

cdhunt commented 7 years ago

Thanks, @jgigler.