嘲弄正在返回Null数组

时间:2019-07-12 17:50:31

标签: powershell pester

我们正在尝试使用Pester测试我们的Powershell代码,该代码将从数据库中获取工作站数据,但是,当我们尝试模拟来自数据库的数据时,模拟模块(invoke-sqlcmd)返回一个空数组。

我们已经尝试过更改数据,重新格式化,但是此当前json文件是从数据库到json的输出副本。

我们还尝试过以其他方式重新格式化模拟,例如:

  • Mock -CommandName Invoke-Sqlcmd {$InvokeSqlcmdallsccmMock}
  • Mock -CommandName Invoke-Sqlcmd -MockWith { return $InvokeSqlcmdallsccmMock}
  • Mock -CommandName Invoke-Sqlcmd { hard coded data}

它仍然会产生相同的空数组错误。

    $DBServer = 'dbname'
    $DBName = 'Name'

    $sql = "SELECT * FROM workstations WHERE v_gs_system = 'workstation'"

    # Reset SCCM list and post-process workstation list
    $SCCM = $null
    $all_workstations = @{}

    # Catch any errors with connecting to db or executing query
    try {
        $SCCM = @(invoke-sqlcmd -query $sql -database $DBName -serverinstance $DBServer)
    }
    catch {
        throw "The sql query could not be run as expected."
    }
    # process the raw data
    foreach($item in $SCCM)
    {   # reset the hash
        try{
        $workstation_hash = @{}
        $workstation_hash.Add("HostName", $item.ItemArray[1])
        $workstation_hash.Add("SerialNumber0", $item.ItemArray[3]
        }catch{

        }
        try {$all_workstations.Add($workstation_hash["Hostname"], $workstation_hash)
        }
        catch {#code}
        }
        try {$all_workstations.Add($workstation_hash["SerialNumber0"], $workstation_hash)
        }
        catch {#code}
        }
    }
    return $all_workstations
}
$JsonMockData = Get-Content -Path $GetAllSCCMworkstationsTestdata -Raw
$InvokeSqlcmdallsccmMock = ConvertFrom-Json $JsonMockData

 Describe "Get-AllSCCMWorkstations" -Tags Build , Unit{
        Context 'Test Group 1 - SCCM Correct Returns' {
            Mock -CommandName Invoke-Sqlcmd { return $InvokeSqlcmdallsccmMock}
            It "Create object for workstation by HostName." -Skip {
                $ws_info = Get-AllSCCMWorkstations                
            }
        }
    }
{
        "invoke-sqlcmd": [
        {
            "6GKSK11111":  [{
                "HostName":  "EDAOCGFETB015",
                "SerialNumber0":  "6GKSK11111"
            }],
            "EDAOCGFETB015":  [{
                "HostName":  "EDAOCGFETB015",
                "SerialNumber0":  "6GKSK11111"
            }]
        }
    ]
}

我们的数据如下:

"HostName","SerialNumber0" #more data but this is the minimum.
"EDAOCGFETB015","6GKSK11111"

我们希望数据存在,但在模拟时不存在。

0 个答案:

没有答案