From da14291078b0558bea8d1abfdad14278b5629928 Mon Sep 17 00:00:00 2001 From: Dave Wyatt Date: Thu, 4 Sep 2014 10:32:31 -0400 Subject: [PATCH] Mocking bug fix Mocking functions with no param block (or mocking executables) was producing single-element $args array, where the element was itself another array. Not sure why; PowerShell's @() operator is not supposed to do that, but the code has been updated to work properly now. --- Functions/Mock.Tests.ps1 | 17 +++++++++++++++++ Functions/Mock.ps1 | 3 ++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Functions/Mock.Tests.ps1 b/Functions/Mock.Tests.ps1 index 9fdc33da9..6a72e2bc4 100644 --- a/Functions/Mock.Tests.ps1 +++ b/Functions/Mock.Tests.ps1 @@ -319,6 +319,23 @@ Describe "When calling Mock on existing function with matching unbound arguments } } +Describe 'When calling Mock on a function that has no parameters' { + function Test-Function { } + Mock Test-Function { return $args.Count } + + It 'Sends the $args variable properly with 2+ elements' { + Test-Function 1 2 3 4 5 | Should Be 5 + } + + It 'Sends the $args variable properly with 1 element' { + Test-Function 1 | Should Be 1 + } + + It 'Sends the $args variable properly with 0 elements' { + Test-Function | Should Be 0 + } +} + Describe "When calling Mock on cmdlet Used by Mock" { Mock Set-Item {return "I am not Set-Item"} Mock Set-Item {return "I am not Set-Item"} diff --git a/Functions/Mock.ps1 b/Functions/Mock.ps1 index 54d3fe8ba..56c865252 100644 --- a/Functions/Mock.ps1 +++ b/Functions/Mock.ps1 @@ -609,7 +609,8 @@ function MockPrototype { $moduleName = $ExecutionContext.SessionState.Module.Name } - $ArgumentList = @(Get-Variable -Name args -ValueOnly -Scope Local -ErrorAction SilentlyContinue) + $ArgumentList = Get-Variable -Name args -ValueOnly -Scope Local -ErrorAction SilentlyContinue + if ($null -eq $ArgumentList) { $ArgumentList = @() } Invoke-Mock -CommandName $functionName -ModuleName $moduleName -BoundParameters $PSBoundParameters -ArgumentList $ArgumentList }