From 0fdb757c36227e6f3f6757c205fa9bec3001902b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20=28Dibildos=29=20Gonz=C3=A1lez?= Date: Fri, 10 Oct 2025 19:55:18 +0200 Subject: [PATCH 1/2] fix(mock): add AsHashtable parameter to MockCallJson and Get-MockFileContentJson --- Test/include/invokeCommand.mock.ps1 | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Test/include/invokeCommand.mock.ps1 b/Test/include/invokeCommand.mock.ps1 index 192b6e4..6672b75 100644 --- a/Test/include/invokeCommand.mock.ps1 +++ b/Test/include/invokeCommand.mock.ps1 @@ -73,13 +73,14 @@ function MockCallAsync{ function MockCallJson{ param( [Parameter(Position=0)][string] $command, - [Parameter(Position=1)][string] $filename + [Parameter(Position=1)][string] $filename, + [Parameter()][switch] $AsHashtable ) Assert-MockFileNotfound $fileName - Set-InvokeCommandMock -Alias $command -Command "Get-MockFileContentJson -filename $filename" + Set-InvokeCommandMock -Alias $command -Command "Get-MockFileContentJson -filename $filename -AsHashtable:$AsHashtable" } function MockCallJsonAsync{ @@ -122,12 +123,13 @@ function Get-MockFileContent{ function Get-MockFileContentJson{ param( - [parameter(Mandatory,Position=0)][string] $fileName + [parameter(Mandatory,Position=0)][string] $fileName, + [Parameter()][switch] $AsHashtable ) Assert-MockFileNotfound $FileName - $content = Get-MockFileContent -fileName $filename | ConvertFrom-Json + $content = Get-MockFileContent -fileName $filename | ConvertFrom-Json -AsHashtable:$AsHashtable -Depth 10 return $content } Export-ModuleMember -Function Get-MockFileContentJson From 7f49b39113f7d9ac40a60bef1a1c0c5aa0b928a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20=28Dibildos=29=20Gonz=C3=A1lez?= Date: Fri, 10 Oct 2025 20:24:35 +0200 Subject: [PATCH 2/2] test(mock): add tests for MockCallJson and MockCallJson_AsHashtable functions --- Test/include/invokeCommand.mock.ps1 | 7 +++++- Test/private/mocks/test.json | 4 ++++ Test/public/invokeCommand.mock.test.ps1 | 32 +++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 Test/private/mocks/test.json diff --git a/Test/include/invokeCommand.mock.ps1 b/Test/include/invokeCommand.mock.ps1 index 6672b75..9fbd479 100644 --- a/Test/include/invokeCommand.mock.ps1 +++ b/Test/include/invokeCommand.mock.ps1 @@ -79,8 +79,13 @@ function MockCallJson{ ) Assert-MockFileNotfound $fileName + $asHashTableString = $AsHashtable ? '$true' : '$false' - Set-InvokeCommandMock -Alias $command -Command "Get-MockFileContentJson -filename $filename -AsHashtable:$AsHashtable" + $commandstr ='Get-MockFileContentJson -filename {filename} -AsHashtable:{asHashTableString}' + $commandstr = $commandstr -replace "{asHashTableString}", $asHashTableString + $commandstr = $commandstr -replace "{filename}", $filename + + Set-InvokeCommandMock -Alias $command -Command $commandstr } function MockCallJsonAsync{ diff --git a/Test/private/mocks/test.json b/Test/private/mocks/test.json new file mode 100644 index 0000000..9dbf5c7 --- /dev/null +++ b/Test/private/mocks/test.json @@ -0,0 +1,4 @@ +{ + "Name": "Test", + "Value": 42 +} \ No newline at end of file diff --git a/Test/public/invokeCommand.mock.test.ps1 b/Test/public/invokeCommand.mock.test.ps1 index e25f49f..df7a0f1 100644 --- a/Test/public/invokeCommand.mock.test.ps1 +++ b/Test/public/invokeCommand.mock.test.ps1 @@ -1,4 +1,36 @@ +function Test_MockCallJson{ + + Reset-InvokeCommandMock + + $fileName = 'test.json' + + MockCallJson -Command 'Test-Command' -filename $fileName + + $result = Invoke-MyCommand -Command 'Test-Command' + + Assert-IsTrue -Condition ($result -is [PSCustomObject]) + Assert-AreEqual -Expected 'Test' -Presented $result.Name + Assert-AreEqual -Expected 42 -Presented $result.Value +} + +function Test_MockCallJson_AsHashtable{ + + Reset-InvokeCommandMock + + $fileName = 'test.json' + + # Act + MockCallJson -Command 'Test-Command' -filename $fileName -AsHashtable + + # Assert invoking mock command + $result = Invoke-MyCommand -Command 'Test-Command' + + Assert-IsTrue -Condition ($result -is [hashtable]) + Assert-AreEqual -Expected 'Test' -Presented $result.Name + Assert-AreEqual -Expected 42 -Presented $result.Value +} + function Test_MockCallToObject{ Reset-InvokeCommandMock