标签: pester

如何使用Pester模拟对exe文件的调用?

在PowerShell中开发脚本,我需要调用外部可执行文件(.exe).目前我正在使用TDD方法开发此脚本,因此我需要模拟调用此.exe文件.

我试试这个:

Describe "Create-NewObject" {
    Context "Create-Object" {
        It "Runs" {
            Mock '& "c:\temp\my.exe"' {return {$true}}
            Create-Object| Should Be  $true
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到了这个回复:

Describing Create-NewObject
   Context Create-Object
    [-] Runs 574ms
      CommandNotFoundException: Could not find Command & "C:\temp\my.exe"
      at Validate-Command, C:\Program Files\WindowsPowerShell\Modules\Pester\Functions\Mock.ps1: line 801
      at Mock, C:\Program Files\WindowsPowerShell\Modules\Pester\Functions\Mock.ps1: line 168
      at <ScriptBlock>, C:\T\Create-NewObject.tests.ps1: line 13
Tests completed in 574ms
Passed: 0 Failed: 1 Skipped: 0 Pending: 0 Inconclusive: 0
Run Code Online (Sandbox Code Playgroud)

有没有办法模拟这种类型的调用而不将它们封装在一个函数中?

powershell unit-testing mocking pester

5
推荐指数
2
解决办法
942
查看次数

Powershell 5类的Pester模拟方法

我在尝试模拟powershell 5类方法时遇到问题,在执行测试时,我收到错误"CommandNotFoundException:找不到Command FunctionToMock".我试图通过模拟"FunctionToMock"来单元测试"OutputToOverwrite"方法.我想我必须首先嘲笑ChocoClass本身,但我不知道该怎么做.谢谢.

Class ChocoClass
{
    [string] OutputToOverwrite()
    {
        return $this.FunctionToMock()
    }

    [string] FunctionToMock()
    {
        return "This text will be replaced"
    }
}


Describe "Testing mocking"{
    it "Mock test"{
        Mock FunctionToMock -MockWith {return "mystring"}
        $package = New-Object ChocoClass
        $expected = $package.OutputToOverwrite()
        $expected | should BeExactly "mystring"
    }
}
Run Code Online (Sandbox Code Playgroud)

powershell class pester

5
推荐指数
1
解决办法
1150
查看次数

Pester 不是模拟点源的函数

我正在使用 Pester 测试一个 PowerShell 脚本,该脚本点源另一个脚本。当我尝试模拟点源函数时,Pester 拒绝使用模拟版本。当我尝试通过将函数添加到 .psm1 文件并使用 Import-Module 而不是点源来获取函数时,我遇到了同样的问题。

这是一个复制我遇到的问题的示例。所有 3 个文件都在同一个文件夹中。

文件.ps1

Function Invoke-Foo{
    'Cantelope'
}
Run Code Online (Sandbox Code Playgroud)

酒吧.ps1

function Invoke-Bar {
    . .\foo.ps1
    Invoke-foo
}
Run Code Online (Sandbox Code Playgroud)

Bar.tests.ps1

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
. "$here\$sut"
. .\Foo.ps1

Describe "Bar" {
    It "Mocks Foo" {
        Mock Invoke-Foo {'Banana'}
        Invoke-Bar | should be 'Banana'
    }
}
Run Code Online (Sandbox Code Playgroud)

在模拟 Invoke-Foo 之后,结果应该是 'Banana',但结果是:

Describing Bar
 [-] Mocks Foo 36ms
   Expected string length 6 but was 9. Strings differ …
Run Code Online (Sandbox Code Playgroud)

powershell unit-testing pester

4
推荐指数
1
解决办法
671
查看次数

Pester 自定义错误文本

有谁知道 Pester 是否支持自定义错误文本?

假设我有以下测试:

Describe "Get-HelloWorld" {
    It "outputs 'Hello world!'" {
        Get-HelloWorld | Should Be 'Hello world!'
    }
}
Run Code Online (Sandbox Code Playgroud)

Get-HelloWorld如果返回“Hello World”以外的另一个字符串,有没有办法可以设置一些自定义错误消息?

谢谢

powershell pester

4
推荐指数
1
解决办法
527
查看次数

Pester示例脚本在Windows 10上获取“ -Be不是有效的Should运算符”,在Ubuntu上运行良好

Pester的新增功能,按照Pester Github页面https://github.com/pester/Pester上的说明在Ubuntu 16.04上安装了Powershell和Pester 。执行了他们的示例脚本Get-Planet.Tests.ps1和Get-Planet.ps1,它的工作与广告一样。按照他们的指示在Windows 10上尝试了相同的操作,并得到“ -Be不是有效的Should运算符”。

由于我是Pester和Powershell的新手,因此不确定在哪里查找问题。这是他们自己的示例,按照其说明安装,没有任何修改。任何帮助将不胜感激。

powershell pester

4
推荐指数
2
解决办法
1390
查看次数

Pester 测试非导出的 PowerShell cmdlet/函数

我有一个可导出一个 cmdlet 的 PowerShell 模块。该模块包含一些最终用户不可见的功能。但是,我想通过 Pester 测试这些功能(因为测试设置很简单)。

是否可以调用 cmdlet 的非导出函数?或者,是否可以强制加载所有函数的模块,尽管 psd1 文件仅导出其中的一些函数?

powershell pester

4
推荐指数
1
解决办法
533
查看次数

我如何 Powershell Pester 测试 ThrowTerminatingError

我如何 Powershell Pester 测试 ThrowTerminatingError ?

catch
{
    $PSCmdlet.ThrowTerminatingError( $PSItem )
}
Run Code Online (Sandbox Code Playgroud)

输出:

Missed command:

File                  Class Function          Line Command
----                  ----- --------          ---- -------
Test-ServerOnline.ps1       Test-ServerOnline   50 $PSCmdlet.ThrowTerminatingError( $PSItem )
Run Code Online (Sandbox Code Playgroud)

testing powershell pester

4
推荐指数
1
解决办法
853
查看次数

Pester Testdrive 不存在

我正在尝试使用 Pesters TestDrive 创建自定义文件管理 powershell 函数的测试。但是,我没有以任何方式运行它,总是收到 TestDrive 不存在的错误。

即使使用文档中的示例: https: //pester.dev/docs/usage/testdrive

我创建了一个文件“pester.tests.ps1”,其中仅包含示例:

function Add-Footer($path, $footer) {
    Add-Content $path -Value $footer
}

Describe "Add-Footer" {
    $testPath = "TestDrive:\test.txt"
    Set-Content $testPath -value "my test text."
    Add-Footer $testPath "-Footer"
    $result = Get-Content $testPath

    It "adds a footer" {
        (-join $result) | Should -Be "my test text.-Footer"
    }
}
Run Code Online (Sandbox Code Playgroud)

出现的错误:

Starting discovery in 1 files. Set-Content : Cannot find drive. A drive with the name 'TestDrive' does not exist. At ...\pester.tests.ps1:7 char:5
+     Set-Content …
Run Code Online (Sandbox Code Playgroud)

powershell pester

4
推荐指数
1
解决办法
1081
查看次数

如何为使用动态参数的高级函数 cmdlet 创建包装器

我正在尝试为 Pester 的 cmdlet 创建一个包装器(代理)Should。可能的用例包括即使在成功的情况下也可以透明地记录测试输入,并改进 Pester 记录某些类型对象的方式,例如hashtable.

作为Should一个高级函数,通过$argssplatting 转发参数不起作用。

所以我尝试使用生成一个包装器,如这个答案System.Management.Automation.ProxyCommand::Create()所述:

$cmd = Get-Command Should
$wrapperSource = [System.Management.Automation.ProxyCommand]::Create( $cmd )
$wrapperSource >should_wrapper.ps1
Run Code Online (Sandbox Code Playgroud)

调用包装器时,Powershell 输出以下错误消息:

应该:无法使用指定的命名参数解析参数集。发出的一个或多个参数不能一起使用或提供的参数数量不足。

看起来包装器生成器不理解 的动态参数声明Should

如何在Should不重复 Pester 代码的情况下为 Pester 编写通用包装器?

powershell wrapper pester

4
推荐指数
1
解决办法
422
查看次数

如何在 Pester 中比较两个数组与自定义对象

我们正在努力让 Pester 测试失败或通过,具体objects取决于array.

测试.ps1

#require Assert
#require Pester

$Expected = @(
    [PSCustomObject]@{Name1 = 'Text1';Name2 = 'Text2'}
    [PSCustomObject]@{Name1 = 'Text1';Name2 = 'Text2'}
)
$Actual = @(
    [PSCustomObject]@{Name1 = 'Text1';Name2 = 'Text2'}
    [PSCustomObject]@{Name1 = 'Text1';Name2 = 'Text2'}
)

Describe 'comparing arrays' {
    Context 'Assert-Equivalent' {
        it 'should be green' {           
            Assert-Equivalent -Actual $Expected -Expected $Expected
        }
        it 'should be green' {        
            Assert-Equivalent -Actual $Actual -Expected $Expected
        }
        it 'should be red' {        
            $Wrong = @(
                [PSCustomObject]@{Name1 = 'Text1';Name2 …
Run Code Online (Sandbox Code Playgroud)

arrays powershell compare pester

3
推荐指数
1
解决办法
2528
查看次数

标签 统计

pester ×10

powershell ×10

unit-testing ×2

arrays ×1

class ×1

compare ×1

mocking ×1

testing ×1

wrapper ×1