From 7598e8180f1ed08dfea3e27c99781079867d159b Mon Sep 17 00:00:00 2001 From: "Shaun Lawrie (via GitHub Actions)" Date: Thu, 14 Mar 2024 23:59:37 +1300 Subject: [PATCH] Fix unit test --- .../prompts/Read-SpectrePause.tests.ps1 | 17 +++++++++-------- PwshSpectreConsole/private/Read-ConsoleKey.ps1 | 3 +++ .../public/prompts/Read-SpectrePause.ps1 | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) create mode 100644 PwshSpectreConsole/private/Read-ConsoleKey.ps1 diff --git a/PwshSpectreConsole.Tests/prompts/Read-SpectrePause.tests.ps1 b/PwshSpectreConsole.Tests/prompts/Read-SpectrePause.tests.ps1 index a0e7de0d..b9bcb0c3 100644 --- a/PwshSpectreConsole.Tests/prompts/Read-SpectrePause.tests.ps1 +++ b/PwshSpectreConsole.Tests/prompts/Read-SpectrePause.tests.ps1 @@ -6,27 +6,28 @@ Describe "Read-SpectrePause" { InModuleScope "PwshSpectreConsole" { BeforeEach { $testMessage = $null - Mock Write-SpectreHost { - if ($testMessage) { - $Message | Should -Be $testMessage - } - } + Mock Write-SpectreHost -Verifiable -ParameterFilter { $Message -eq $testMessage } + Mock Write-SpectreHost { } Mock Clear-InputQueue Mock Set-CursorPosition Mock Write-Host - Mock Read-Host + Mock Read-ConsoleKey { + $enter = [System.ConsoleKey]::Enter + return [System.ConsoleKeyInfo]::new([char]$enter.value__, $enter, $false, $false, $false) + } } It "displays" { Read-SpectrePause - Assert-MockCalled -CommandName "Read-Host" -Times 1 -Exactly + Assert-MockCalled -CommandName "Read-ConsoleKey" -Times 1 -Exactly } It "displays a custom message" { $testMessage = Get-RandomString Write-Debug $testMessage Read-SpectrePause -Message $testMessage - Assert-MockCalled -CommandName "Read-Host" -Times 1 -Exactly + Assert-MockCalled -CommandName "Read-ConsoleKey" -Times 1 -Exactly + Should -InvokeVerifiable } } } \ No newline at end of file diff --git a/PwshSpectreConsole/private/Read-ConsoleKey.ps1 b/PwshSpectreConsole/private/Read-ConsoleKey.ps1 new file mode 100644 index 00000000..2f02c861 --- /dev/null +++ b/PwshSpectreConsole/private/Read-ConsoleKey.ps1 @@ -0,0 +1,3 @@ +function Read-ConsoleKey { + return [console]::ReadKey($true) +} \ No newline at end of file diff --git a/PwshSpectreConsole/public/prompts/Read-SpectrePause.ps1 b/PwshSpectreConsole/public/prompts/Read-SpectrePause.ps1 index b04985ac..698a3cc8 100644 --- a/PwshSpectreConsole/public/prompts/Read-SpectrePause.ps1 +++ b/PwshSpectreConsole/public/prompts/Read-SpectrePause.ps1 @@ -39,7 +39,7 @@ function Read-SpectrePause { } Write-SpectreHost $Message -NoNewline do { - $key = [console]::ReadKey($true) + $key = Read-ConsoleKey if($AnyKey) { break }