我有许多模块,包括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
请注意以下事项:
- 我没有导入定义了-Sql函数的 ModuleSql,因为无论如何我都是在模拟它们。
- 我观察到/发现我需要设置为定义Main-Function
-ModuleName
的模块,而不是定义 SQL 函数(我试图模拟)的模块。 - 我玩过
InModuleScope
and-ModuleName
,例如将一个或另一个设置为ModuleSQL,但主要是让事情变得更糟。
通过在其他模拟函数中添加 Verbose 输出,我已经确认New-SqlConnection
和Close-SqlConnection
都被拦截了,但Invoke-SqlStoredProc
并Invoke-SqlQuery
没有。
Invoke-Sql*
深入探索,我可以看到(模拟的)函数引发了以下异常:错误:“从'System.Boolean'到'System.Data.SqlClient.SqlConnection'的无效转换。” 这是我在调用这些函数的真实版本时所期望的行为,但我期望模拟版本会忽略参数类型。
为什么 Pester 只拦截我的 4 个函数中的 2 个?