diff --git a/Test/include/invokeCommand.mock.ps1 b/Test/include/invokeCommand.mock.ps1 index 192b6e4..9fbd479 100644 --- a/Test/include/invokeCommand.mock.ps1 +++ b/Test/include/invokeCommand.mock.ps1 @@ -73,13 +73,19 @@ 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 + $asHashTableString = $AsHashtable ? '$true' : '$false' + + $commandstr ='Get-MockFileContentJson -filename {filename} -AsHashtable:{asHashTableString}' + $commandstr = $commandstr -replace "{asHashTableString}", $asHashTableString + $commandstr = $commandstr -replace "{filename}", $filename - Set-InvokeCommandMock -Alias $command -Command "Get-MockFileContentJson -filename $filename" + Set-InvokeCommandMock -Alias $command -Command $commandstr } function MockCallJsonAsync{ @@ -122,12 +128,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 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