From 1ecfbc6336647cc13923b57f8f1e2904519e1a1d Mon Sep 17 00:00:00 2001 From: Dave Wyatt Date: Sun, 3 Aug 2014 20:51:10 -0400 Subject: [PATCH] Describe and Context fix Fixture parameters to Describe was mistakenly marked as Mandatory in a recent commit. Instead of that, its previous behavior was to be optional, but to throw a more user-friendly error message if it's not specified. Added tests to make sure this doesn't happen again, and also applied the same logic / tests to the Context command. --- Functions/Context.Tests.ps1 | 18 ++++++++++++++++++ Functions/Context.ps1 | 4 ++-- Functions/Describe.Tests.ps1 | 18 ++++++++++++++++++ Functions/Describe.ps1 | 3 ++- 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 Functions/Context.Tests.ps1 create mode 100644 Functions/Describe.Tests.ps1 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?)") )