- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有很多模块,包括 ModuleMain 和 ModuleSql。模块之间存在相互依赖性,因此 ModuleMain 中的 Main-Function 使用了 ModuleSql 中的 4 个函数:
function Main-Function {
[CmdletBinding(SupportsShouldProcess=$true)]
# The following 4 lines are all wrapped in $PSCmdlet.ShouldProcess() and
# try {} catch {} logic. I have left that out in this post, but I mention
# it in case it is relevant.
$a = New-SqlConnection
$b = Invoke-SqlStoredProc -connection $a
$c = Invoke-SqlQuery -connection $a
Close-SqlConnection -connection $a | Out-Null
return $c
}
我创建了一个 Function-Main1.tests.ps1 文件来测试 Function-Main1。起初我使用 InModuleScope
但后来切换到使用 -ModuleName
参数指定每个模拟的模块。
Import-Module "ModuleMain" -Force
Describe "Main-Function" {
Mock -ModuleName ModuleMain New-SqlConnection {
return $true }
Mock -ModuleName ModuleMain Invoke-SqlStoredProc {
return $true }
Mock -ModuleName ModuleMain Invoke-SqlQuery {
return $true }
Mock -ModuleName ModuleMain Close-SqlConnection {
return $true }
Context "When calling Main-Function with mocked SQL functions" {
It "Calls each SQL function once" {
Assert-MockCalled -Scope Context -ModuleName ModuleMain -CommandName New-SqlConnecion -Times 1 -Exactly
Assert-MockCalled -Scope Context -ModuleName ModuleMain -CommandName Invoke-SqlStoredProc -Times 1 -Exactly
Assert-MockCalled -Scope Context -ModuleName ModuleMain -CommandName Invoke-SqlQuery -Times 1 -Exactly
Assert-MockCalled -Scope Context -ModuleName ModuleMain -CommandName Close-SqlConnecion -Times 1 -Exactly
}
}
}
当我运行这个测试时,我得到以下结果:
[-] Calls each SQL function once 223ms
Expected Invoke-SqlStoredProc in module ModuleMain to be called 1 times exactly but was called 0 times
at line: xx in
xx: Assert-MockCalled -Scope Context -ModuleName ModuleMain -CommandName Invoke-SqlStoredProc -Times 1 -Exactly
注意以下几点:
-ModuleName
设置为定义 Main-Function 的模块,而不是 SQL 函数(即我正在尝试模拟)已定义。InModuleScope
和 -ModuleName
,例如将一个或另一个设置为 ModuleSQL,但主要是这只会让事情变得更糟。通过尝试,在其他模拟函数中添加 Verbose 输出,我已经确认 New-SqlConnection
和 Close-SqlConnection
都被拦截了,但是 Invoke-SqlStoredProc
和 Invoke-SqlQuery
不是。
深入探索,我可以看到 Invoke-Sql*
(模拟)函数正在抛出以下异常:Error: "Invalid cast from 'System.Boolean' to ' System.Data.SqlClient.SqlConnection'。”这是我在调用这些函数的真实版本时期望的行为,但我期望模拟版本会忽略参数类型.
为什么 Pester 只会拦截我的 4 个函数中的 2 个?
最佳答案
所以,上面的评论中给出了这个问题的简短答案:
Mocked functions does not ignore parameter types. --PetSerAl
这意味着当我尝试调用 Invoke-Sql*
函数并使用虚假的 $sqlConnection 变量(只需设置为 $true) 这导致了错误,因为输入参数不是预期的数据类型。
我的解决方案是模拟 New-SqlConnection
函数,因此它返回一个 [System.Data.SqlCient.SqlConnection]
对象。碰巧的是,我也恢复使用 InModuleScope
而不是在每个 Mock 上指定模块:
InModuleScope "ModuleMain" {
Describe "MainFunction" {
Mock New-SqlConnection {
New-Object -TypeName System.Data.SqlCient.SqlConnection
}
...
}
关于unit-testing - Pester:并非所有模拟函数都被拦截,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36082850/
我正在尝试为我的 Azure 自动化 Runbook 编写 Pester 测试。 Runbook 脚本使用 Get-AutomationVariable cmdlet,我尝试通过以下方式模拟它: mo
我正在尝试为我的 Azure 自动化 Runbook 编写 Pester 测试。 Runbook 脚本使用 Get-AutomationVariable cmdlet,我尝试通过以下方式模拟它: mo
我有一套 pester测试使用 v4.0+ should 运算符 -FileContentMatch 的 PowerShell 模块。当这些测试在装有较早 v3.x 版本 pester 的机器上运行时
function Palindrome1 { [CmdletBinding()] param ( [Parameter(Mandatory)] [str
我们正在编写 Pester 测试来测试 Azure 资源组是否包含某些标签。以下是脚本,不幸的是,即使我们正在检查的特定资源组不包含某些标签(来自定义的数组),Pester 测试也没有报告任何失败。
我正在尝试使用Pesters TestDrive为自定义文件管理Powershell函数创建测试。 但是,我没有让它以任何方式运行,总是会收到TestDrive不存在的错误。 即使使用文档中的示例:h
我正在测试 PowerShell 脚本。我想在不执行整个脚本的情况下测试单个函数。我不确定这是预期的用例还是受支持的,并且我在网上搜索时找不到好的答案 sut.ps1: Param( [Par
我们正在编写 Pester 测试来测试 Azure 资源组是否包含某些标签。以下是脚本,不幸的是,即使我们正在检查的特定资源组不包含某些标签(来自定义的数组),Pester 测试也没有报告任何失败。
当我运行以下纠缠测试时,我希望它能够捕获预期的错误,但它没有。但是,当我使用不同的 throw 语句使用不同的函数运行测试时,它会起作用。 纠缠测试: Describe "Remove-Generic
如何unmock 以前模拟的函数?有时我发现自己处于一种情况,我想测试我之前mocked 的函数。 一个简化的例子: Describe 'Pester mocking' { $testFile
我们正在尝试评估 Invoke-Command被调用了一次。 脚本.ps1 Get-Job | Remove-Job Invoke-Command -ScriptBlock {'test'} -Com
Invoke-Pester命令可以使用 -Script 调用带有显式参数的单个测试脚本。范围。但是如果我想将相同的参数传递给所有的测试脚本呢? 我不想在循环中调用 peter,因为我希望它生成单个测试
我有一个 Pester 脚本,正在为我的 API 运行一些冒烟测试。当我运行 Invoke-Pester 时,我得到了 Logo 并且测试运行良好。但是在日志的末尾我得到 Tests Passed:
我们正在努力让 Pester 测试失败或通过,这取决于 array 中的 objects 是否相等。 测试.ps1 #require Assert #require Pester $Expected
我想知道为什么在运行此脚本时会出现以下行为。我在 PowerShell ISE(v4 主机)中加载了脚本并加载了 Pester 模块。我按 F5 运行脚本。 function Test-Pester
我正在尝试找出如何对缺少的参数进行 Pester 测试: Find-Waldo.Tests.ps1 $here = Split-Path -Parent $MyInvocation.MyCommand
在 nUnit 中,我们可以这样做: Expect(actualColleciton, EquivalentTo(expectedCollection)); 和 Expect(actualCollec
我有很多模块,包括 ModuleMain 和 ModuleSql。模块之间存在相互依赖性,因此 ModuleMain 中的 Main-Function 使用了 ModuleSql 中的 4 个函数:
我正在使用 Pester 测试一个 PowerShell 脚本,该脚本点源另一个脚本。当我尝试模拟点源函数时,Pester 拒绝使用模拟版本。当我尝试通过将函数添加到 .psm1 文件并使用 Impo
编辑 问题的症结在于:如何访问在我的 It block 中的 BeforeDiscovery block 中声明的变量,这些变量没有被it -foreach $var 构造? 我很难适应 Pester
我是一名优秀的程序员,十分优秀!