简体   繁体   English

Powershell Azure Pester测试

[英]Powershell Azure Pester Test

Below is a simple function that just creates a resource group and exports the data. 下面是一个简单的函数,它仅创建一个资源组并导出数据。 I am trying to learn unit test but I cant seem to figure it out. 我正在尝试学习单元测试,但似乎无法弄清楚。

Is it possible to give the test mock data? 是否可以提供测试模拟数据? and can I test if the output file would work? 我可以测试输出文件是否可以工作吗?

function New-AzureRG{

    param([string]$rgName,
     [string]$location

    )

$getData =  New-AzureRmResourceGroup -Name $rgName -location 'WestEurope'

$getData | Export-Csv $location

}


Describe "New-AzureRG" {
    Context "Function Exists" {
        It "Should return a message" {
        $sum = New-AzureRG -rgName testRG -location C:\tst\testsc.csv
        ($um).Name | Should Be "testRG"
        }
    }
}

Here is my terrible attempt to make a test using pester. 这是我用杵进行测试的可怕尝试。 For some reason the test is actually doing it, instead of making it as a test. 由于某种原因,测试实际上是在做,而不是将其作为测试。 Im just confused :(. 我只是感到困惑:(。

I do not think your test is actually working. 我认为您的测试实际上没有作用。 For example in the test $um is not assigned... If you mock the New-AzureRG function in your current test you are testing nothing. 例如,在测试$ um中未分配...如果您在当前测试中模拟New-AzureRG函数,则表示您未进行任何测试。 I guess you want something like: 我想你想要类似的东西:

Make a function c 使功能c

Call the function from an other function 从其他函数调用该函数

Mock the New-AzureRG function in your test 在测试中模拟New-AzureRG功能

You mock can look something like: 您的模拟可以看起来像:

Mock New-AzureRG { return @{Name = "NameRG"} } -ParameterFilter { $Name -eq "NameRG" }

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

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