嘲笑佩斯特

发布于 2025-02-10 05:23:09 字数 855 浏览 3 评论 0原文

我对Powershell和Pester是一个新手。我遵循了一些有关佩斯特和如何创建测试的培训,但我对此并不十分清楚。我的功能很小,可以检查在线PDF的哈希,并且已经为此写了一个测试,但是我不确定我是否以正确的方式进行操作。感谢任何帮助/解释。

测试的功能:

function Get-FileHashFromUrl {
    try {
        $wc = [System.Net.WebClient]::new()
        Get-FileHash -InputStream($wc.OpenRead("https://example.com/apps/info.pdf"))
    }
    catch {
        return $Error
    }
}

用perster 5.3 测试

BeforeAll {
    . $PSScriptRoot\myScript.ps1
}
Describe "Get-FileHashFromUrl" {
    It 'Get the PDF Hash from Url' {
        Mock -CommandName Get-FileHashFromUrl -MockWith { $hash }
        $hash = '7EE8DB731BF3E5F7CF4C8688930D1EB079738A3372EE18C118F9F4BA22064411'
        Get-FileHashFromUrl | Should -Be $hash
        Assert-MockCalled Get-FileHashFromUrl -Times 1 -Exactly
    }
}

I am an utter newbie regarding PowerShell and Pester. I followed some trainings about Pester and how to create tests but I am not very clear about it. I have a small function that checks the Hash of an online PDF and I have written a test for it but I am not sure if I am doing it the right way. I would appreciate any help/explanation.

Function to Test:

function Get-FileHashFromUrl {
    try {
        $wc = [System.Net.WebClient]::new()
        Get-FileHash -InputStream($wc.OpenRead("https://example.com/apps/info.pdf"))
    }
    catch {
        return $Error
    }
}

Test with Perster 5.3

BeforeAll {
    . $PSScriptRoot\myScript.ps1
}
Describe "Get-FileHashFromUrl" {
    It 'Get the PDF Hash from Url' {
        Mock -CommandName Get-FileHashFromUrl -MockWith { $hash }
        $hash = '7EE8DB731BF3E5F7CF4C8688930D1EB079738A3372EE18C118F9F4BA22064411'
        Get-FileHashFromUrl | Should -Be $hash
        Assert-MockCalled Get-FileHashFromUrl -Times 1 -Exactly
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亚希 2025-02-17 05:23:09

我不确定我会使用 mock 在测试中函数,因为模拟允许您使用要伪造或更改现有命令的行为/返回数据,我想您想真正测试文件hash而不是伪造它。

如果您想故意测试故障(例如,通过获取模拟命令返回错误的哈希)或模拟无法在特定环境中运行的命令,则模拟会很有用。

假设删除了模拟函数,您的测试将无法正常工作,因为您的get-filehashfromurl函数正在返回get-filehash返回的完整对象,而不是返回哈希:

“文件哈希对象”

您要么想更改功能以返回哈希,例如:

function Get-FileHashFromUrl {
    try {
        $wc = [System.Net.WebClient]::new()
        return (Get-FileHash -InputStream($wc.OpenRead("https://example.com/apps/info.pdf"))).Hash
    }
    catch {
        return $Error
    }
}

或,将测试更改为:

BeforeAll {
    . $PSScriptRoot\myScript.ps1
}
Describe "Get-FileHashFromUrl" {
    It 'Get the PDF Hash from Url' {
        $hash = '7EE8DB731BF3E5F7CF4C8688930D1EB079738A3372EE18C118F9F4BA22064411'
        (Get-FileHashFromUrl).Hash | Should -Be $hash
    }
}

I'm not sure I'd be using the Mock function in your test as mocking allows you to fake or alter the behavior/returned data of an existing command and I'm guessing you want to really test the file hash returned rather that faking it.

Mocking would be useful if you wanted to purposely test a failure for example (by getting the mocked command to return the wrong hash), or to simulate a command that you can't run in a particular environment.

Assuming that the mocked function was removed, your test wouldn't work quite as expected because your Get-FileHashFromUrl function is returning the complete object that Get-FileHash returns rather than just the hash:

File hash object

You'd either want to change your function to return just the hash, like so:

function Get-FileHashFromUrl {
    try {
        $wc = [System.Net.WebClient]::new()
        return (Get-FileHash -InputStream($wc.OpenRead("https://example.com/apps/info.pdf"))).Hash
    }
    catch {
        return $Error
    }
}

Or, change your test to:

BeforeAll {
    . $PSScriptRoot\myScript.ps1
}
Describe "Get-FileHashFromUrl" {
    It 'Get the PDF Hash from Url' {
        $hash = '7EE8DB731BF3E5F7CF4C8688930D1EB079738A3372EE18C118F9F4BA22064411'
        (Get-FileHashFromUrl).Hash | Should -Be $hash
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文