简体   繁体   English

如何模拟函数以使用Pester返回复杂值

[英]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. 我的瘟疫代码存在问题,其中我应模拟某个函数( Get-State )以返回复杂值,以便$StartState可以具有值。 With my level of Powershell I can't seem to create a custom object. 就我的Powershell级别而言,我似乎无法创建自定义对象。

    $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 我尝试使用带有此代码的自定义对象来模拟Get-State函数

    $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? 但这并不能解决问题, $StartState仍然没有任何价值,因为也许我必须在哪里创建自定义方法?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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