Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions Test/include/invokeCommand.mock.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Test/private/mocks/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Name": "Test",
"Value": 42
}
32 changes: 32 additions & 0 deletions Test/public/invokeCommand.mock.test.ps1
Original file line number Diff line number Diff line change
@@ -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
Expand Down