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:

$values = 81, 9801, 60025, 3686400, 212255761, 475316482624 
$testCases = foreach ($value in $values) { 
    @{ Value = $value; ExpectedResult = [Math]::Sqrt($value) } 
} 
It 'Calculates the square root of <Value> to be <ExpectedResult>' -TestCases $testCases { 
    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 was: {14.142135623730950488016887242}
At ...

It may be possible to overcome the limitation of the verification by converting both to the same data type. Whether or not 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
3.144.222.185