Independent verification

It is common to find that there is more than one way to achieve a result in PowerShell. In the case of the Get-SquareRoot function, .NET has a Math.Sqrt static method that can be used to produce a similar result.

The availability of an alternative approach (which is known to work) allows a result to be dynamically validated, either in place of, or in addition to, statically defined values.

The set of test cases might be adjusted to use Math.Sqrt to verify that the function is working as intended:

Describe Get-SquareRoot {
It 'When the value is <Value>, the square root is <ExpectedResult>' -TestCases @(
@{ Value = 81; ExpectedResult = [Math]::Sqrt(81) }
@{ Value = 9801; ExpectedResult = [Math]::Sqrt(9801) }
@{ Value = 3686400; ExpectedResult = [Math]::Sqrt(3686400) }
@{ Value = 212255761; ExpectedResult = [Math]::Sqrt(212255761) }
@{ Value = 475316482624; ExpectedResult = [Math]::Sqrt(475316482624) }
) {
param (
$Value,
$ExpectedResult
)

Get-SquareRoot $Value | Should -Be $ExpectedResult
}
}

Independent verification has limitations if two approaches return different data types. For example, the following assertion will fail, despite using the same input values:

PS> Get-SquareRoot 200 | Should -Be ([Math]::Sqrt(200))
Expected 14.142135623731, but got 14.142135623730950488016887242.
At ...

It may be possible to overcome the limitation of the verification by converting both to the same data type. Whether this action is appropriate depends on the nature of, and reason for, the test.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.104.5