Add unit tests to tasks
- Dominant language
- PowerShell
- Stars
- 243
- Forks
- 46
- Avg merge
- 1h 41m
- Merged PRs (30d)
- 1
Description
We should add unit tests to all the tasks.
Example unit tests for a task below. It was copied here due to the original was changed in a way that work would have been lost. So to prevent to reinvent the wheel this is tests that could be reused in this module.
```
Describe 'Generate_Conceptual_Help' {
BeforeAll {
Mock -CommandName New-DscResourcePowerShellHelp
# Stub function for the executable GitVersion.
function gitversion
{
# Make sure to throw if this stub is called.
throw 'GitVersion Mock: Not Implemented'
}
}
BeforeEach {
<#
Make sure we didn't inherit a value for this parameter from the
parent scope or a prior test. If this variable is set to a value
by the build pipeline then this test would fail. This row make sure
we always start clean.
#>
$ModuleVersion = $null
}
It 'Should run the build script alias' {
$buildTaskName = 'Generate_Conceptual_Help'
$buildScriptAliasName = 'Task.{0}' -f $buildTaskName
$script:buildScript = Get-Command -Name $buildScriptAliasName -Module $script:projectName
$script:buildScript.Name | Should -Be $buildScriptAliasName
$script:buildScript.ReferencedCommand | Should -Be ('{0}.build.ps1' -f $buildTaskName)
}
It 'Should dot-source the build script without throwing' {
{ . $script:buildScript } | Should -Not -Throw
}
Context 'When the property ModuleVersion uses a value from the parent scope' {
BeforeAll {
# This mocks the alias 'property' for the parameter 'ModuleVersion'.
Mock -Command Get-BuildProperty -MockWith {
return '99.1.1-preview0001'
} -ParameterFilter {
$Name -eq 'ModuleVersion'
}
$mockTaskParameters = @{
ProjectName = 'MyModule'
SourcePath = $TestDrive
}
$mockExpectedDestinationModulePath = Join-Path -Path $script:projectPath -ChildPath 'output' |
Join-Path -ChildPath $mockTaskParameters.ProjectName |
Join-Path -ChildPath '99.1.1'
}
It 'Should run the build task with the correct destination module path and without throwing' {
{
Invoke-Build -Task $buildTaskName -File $script:buildScript.Definition @mockTaskParameters
} | Should -Not -Throw
Assert-MockCalled -CommandName New-DscResourcePowerShellHelp -ParameterFilter {
$DestinationModulePath -eq $mockExpectedDestinationModulePath
} -Exactly -Times 1 -Scope It
}
}
Context 'When the executable gitversion is available' {
BeforeAll {
# This mocks the executable gitversion.
Mock -CommandName gitversion -MockWith {
# Mock the JSON object that GitVersion returns.
return '
{
"NuGetVersionV2":"99.1.1-preview0001"
}
'
}
$mockTaskParameters = @{
ProjectName = 'MyModule'
SourcePath = $TestDrive
}
$mockExpectedDestinationModulePath = Join-Path -Path $script:projectPath -ChildPath 'output' |
Join-Path -ChildPath $mockTaskParameters.ProjectName |
Join-Path -ChildPath '99.1.1'
}
It 'Should run the build task with the correct destination module path and without throwing' {
{
Invoke-Build -Task $buildTaskName -File $script:buildScript.Definition @mockTaskParameters
} | Should -Not -Throw
Assert-MockCalled -CommandName New-DscResourcePowerShellHelp -ParameterFilter {
$DestinationModulePath -eq $mockExpectedDestinationModulePath
} -Exactly -Times 1 -Scope It
}
}
Context 'When the executable gitversion is not available' {
BeforeAll {
# This mocks the executable gitversion.
Mock -CommandName gitversion -MockWith {
throw
}
Mock -CommandName Get-BuiltModuleVersion -MockWith {
return '1.0.0'
}
$mockTaskParameters = @{
ProjectName = 'MyModule'
SourcePath = $TestDrive
}
$mockExpectedDestinationModulePath = Join-Path -Path $script:projectPath -ChildPath 'output' |
Join-Path -ChildPath $mockTaskParameters.ProjectName |
Join-Path -ChildPath '1.0.0'
}
It 'Should run the build task with the correct destination module path and without throwing' {
{
Invoke-Build -Task $buildTaskName -File $script:buildScript.Definition @mockTaskParameters
} | Should -Not -Throw
Assert-MockCalled -CommandName New-DscResourcePowerShellHelp -ParameterFilter {
$DestinationModulePath -eq $mockExpectedDestinationModulePath
} -Exactly -Times 1 -Scope It
}
}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.