I understand the Theories but need a little help in practice when it comes to testing.
So I'm still in the process of learning testing in general and after some research I can see that the code coverage is generated by how much each line of code is tested. I'm also learning c# sharp side by side and improving my scripting skills as much as possible in the process. I also understand that you usually want to mock out any dependencies when it comes to unit testing a method that relies on another method or class. In this case it's a function or cmdlet since it's powershell.(Learning these side by side shows how much more I can do in c#)
All I'm asking for is a little help identifying things that need to be mocked and how many tests should I write? I know that sounds subjective but I'm trying to get a general idea and best practice for myself. I keep hearing from my co-workers that TDD is the way to go and it does speed up the development/scripting process significantly by catching bugs early that you're going to run in to anyway but don't have to test manually. I'm trying to learn to do tests first but need a little bit of guidance. I apologize if I seem like a scrub. I just need validation that I'm learning correctly.
To make my questions clearer:
Am I identifying what I need to mock correctly?
What am I missing or could identify better?
Am I identifying what I need to test correctly?
What I'm looking for in an answer:
Here's what you're Missing/Screwed up/Could identify better
The Code I'm Testing
Function 1
function New-ServiceSnapshot {
[CmdletBinding()]
param (
# path to export to
[Parameter(Mandatory=$true)]
[string]$ServiceExportPath
)
$results = get-service
$results | Export-Csv -Path $ServiceExportPath -NoTypeInformation
}
On function 1 at first glance I look at it and can see 2 dependencies. I would want to mock out get-service and use dummy data to do this. I did it by an import-csv from real dummy data in to the real object that would be tested. In order to test the export I have no clue. I would maybe mock out the return for the input ExportPath parameter? Just return the string path that was input? Would that suffice?
Function 2
function Inspect-ServiceSnapshot {
[CmdletBinding()]
param (
#Snapshot
[Parameter(Mandatory=$false)]
[ValidateScript({Test-Path $_ -PathType Leaf})]
[string]$SnapshotPath,
# timer
[Parameter(Mandatory=$false)]
[int]$TimeToWait = 0,
# If variable
[Parameter(Mandatory=$false)]
[object[]]
$SnapshotVariable,
# compare variable
[Parameter(Mandatory=$false)]
[object[]]
$CompareVariable
)
if($TimeToWait) { Start-Sleep -Seconds $TimeToWait }
if($SnapshotVariable){$snap = $SnapshotVariable}
if($SnapShotPath){$snap = Import-Csv -Path $SnapshotPath}
if($CompareVariable){$Compare = $CompareVariable}else{$Compare = (Get-Service)}
if($null -eq $CompareVariable -and $null -ne $SnapshotVariable){Write-Error -Message "If you have a SnapshotVariable, Compare variable is required" -ErrorAction Stop}
if($null -eq $SnapshotVariable -and $null -ne $CompareVariable){Write-Error -Message "If you have a CompareVariable, SnapshotVariable is required" -ErrorAction Stop}
$list = @()
foreach($entry in $Compare) {
# make sure we are dealing with the SAME service
$previousStatus = $snap | Where-Object { $_.Name -eq $entry.Name} | Select-Object -Property Status -first 1 -ExpandProperty Status
$info = New-Object -TypeName psobject -Property @{
Name = $entry.Name
CurrentStatus = $entry.status
DisplayName = $entry.displayname
StartType = $entry.StartType
PreviousStatus = if ($null -ne $previousStatus) { $previousStatus } else { 'Didnt Exist before, New Service' }
isDifferent = if($previousStatus -ne $entry.status) {$true} elseif($null -eq $previousStatus) {$null} else { $false}
}
$list += $info
}
$list | Where-Object { $_.isDifferent -eq $true -and $_.StartType -ne "Automatic"}
}
On this I think I would want to test all 4 of the inputs. I also would have to mock the dependencies and some of the inputs since they require real objects for dummy data. Other than that. No Clue.
Aucun commentaire:
Enregistrer un commentaire