简体   繁体   中英

How to Mock function to return a complex value with Pester

I have a problem with my pester code in which I should mock a certain function( Get-State ) to return a complex value so that $StartState can have a value. With my level of Powershell I can't seem to create a custom object.

    $State  = Get-State

    $StartState = $State.Where({$_.Name -eq "State_SUccess"}).state

I tried using a custom object with this code to mock the Get-State function

    $State = [PSCustomObject]@{Name = "State_SUccess"}
    if($State.Name -eq "State_SUccess""){
    $State = [PSCustomObject]@{Name = @{state = 1}}
    }
    else
    {
    }
    $BatchState.statusName.state

But it does not do the trick, the $StartState still has no value because maybe I have to create a custom method where?

I don't have much info on what you are planning to do but i think it is something like this:

Change the first part to:

$State  = Get-State

$StartState = $State.Where({$_.Name -eq "State_SUccess"})

and:

$State = [PSCustomObject]@{Name = "State_SUccess"}
if($State.Name -eq "State_SUccess")
{
    $State = [PSCustomObject]@{Name = @{state = 1}}
}
else
{
}
$StartState.Name

this a rather strange approach... maybe you need something like this:

$State = @{Name = "State_SUccess"}
if($State.Name -eq "State_SUccess")
{
    $State['State'] = 1
}
else
{
}
$StartState

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM