diff --git a/Functions/Context.Tests.ps1 b/Functions/Context.Tests.ps1 new file mode 100644 index 000000000..f31a12076 --- /dev/null +++ b/Functions/Context.Tests.ps1 @@ -0,0 +1,18 @@ +Set-StrictMode -Version Latest + +Describe 'Testing Context' { + It 'Has a non-mandatory fixture parameter which throws the proper error message if missing' { + $command = Get-Command Context -Module Pester + $command | Should Not Be $null + + $parameter = $command.Parameters['Fixture'] + $parameter | Should Not Be $null + + $attribute = $parameter.Attributes | Where-Object { $_.TypeId -eq [System.Management.Automation.ParameterAttribute] } + $isMandatory = $null -ne $attribute -and $attribute.Mandatory + + $isMandatory | Should Be $false + + { Context Bogus } | Should Throw 'No test script block is provided' + } +} diff --git a/Functions/Context.ps1 b/Functions/Context.ps1 index 104315796..dcaa26be7 100644 --- a/Functions/Context.ps1 +++ b/Functions/Context.ps1 @@ -37,8 +37,8 @@ param( [Parameter(Mandatory = $true)] $name, - [Parameter(Mandatory = $true)] - [ScriptBlock] $fixture + [ValidateNotNull()] + [ScriptBlock] $fixture = $(Throw "No test script block is provided. (Have you put the open curly brace on the next line?)") ) $Pester.EnterContext($name) $TestDriveContent = Get-TestDriveChildItem diff --git a/Functions/Describe.Tests.ps1 b/Functions/Describe.Tests.ps1 new file mode 100644 index 000000000..dfeab7926 --- /dev/null +++ b/Functions/Describe.Tests.ps1 @@ -0,0 +1,18 @@ +Set-StrictMode -Version Latest + +Describe 'Testing Describe' { + It 'Has a non-mandatory fixture parameter which throws the proper error message if missing' { + $command = Get-Command Describe -Module Pester + $command | Should Not Be $null + + $parameter = $command.Parameters['Fixture'] + $parameter | Should Not Be $null + + $attribute = $parameter.Attributes | Where-Object { $_.TypeId -eq [System.Management.Automation.ParameterAttribute] } + $isMandatory = $null -ne $attribute -and $attribute.Mandatory + + $isMandatory | Should Be $false + + { Describe Bogus } | Should Throw 'No test script block is provided' + } +} diff --git a/Functions/Describe.ps1 b/Functions/Describe.ps1 index 588eeb107..6ac38d8bd 100644 --- a/Functions/Describe.ps1 +++ b/Functions/Describe.ps1 @@ -54,7 +54,8 @@ about_TestDrive param( [Parameter(Mandatory = $true, Position = 0)] $name, $tags=@(), - [Parameter(Mandatory = $true, Position = 1)] + [Parameter(Position = 1)] + [ValidateNotNull()] [ScriptBlock] $fixture = $(Throw "No test script block is provided. (Have you put the open curly brace on the next line?)") )