我已经创建了一个 PowerShell 测试脚本,Common.tests.ps1
使用 Pester 针对Common.ps1
同一目录中的 PowerShell 脚本中的某些函数,。
在同一目录中还有一个TestInitializer.ps1
脚本,它使用该模块在Dynamics CRM实例Microsoft.Xrm.Data.PowerShell
中创建和获取记录。
从 Visual Studio 运行 PowerShell 测试脚本时,测试在测试资源管理器中失败并显示以下消息:
CommandNotFoundException:无法加载模块“Microsoft.Xrm.Data.PowerShell”。有关详细信息,请运行“导入模块 Microsoft.Xrm.Data.PowerShell”。
但是,从 PowerShell ISE 运行相同的测试时,运行没有问题。这似乎好像没有为 Visual Studio 运行的实例安装模块(我在运行时确认了这一点,Get-Module -ListAvailable
并看到输出不包含Microsoft.Xrm.Data.PowerShell
用于 Visual Studio 测试的模块),尽管即使是像Import-Module Microsoft.Xrm.Data.PowerShell -Global -Force
don'这样的显式调用t 似乎在使用 Visual Studio 执行脚本期间加载模块。
这是Common.test.ps1
:
$here = (Split-Path -Parent $MyInvocation.MyCommand.Path)
. $here\Common.ps1
. $here\TestInitializer.ps1
Describe "SelectionToSingleCharString" {
Context "StringTransforms" {
It "Retrieves a CRM record and uses the optionset value to retrieve a single character" {
SelectionToSingleCharString($crmRecord.new_type) | Should Be "I"
}
}
}
片段来自TestInitializer.ps1
:
# Whether or not this is uncommented does not matter
#Import-Module "$env:SystemRoot\System32\WindowsPowerShell\v1.0\Modules\Microsoft.Xrm.Data.PowerShell\Microsoft.Xrm.Data.PowerShell.psd1" -Global -Force
#$modules = Get-Module -ListAvailable
#Write-Host $modules
# Failing here
Microsoft.Xrm.Data.PowerShell\Connect-CrmOnPremDiscovery -ServerUrl $loginServerUrl -OrganizationName $loginOrgName -Credential $cred
我可以将测试设计为使用 Mock 而不是实际尝试创建/读取记录,尽管无法加载外部模块并在 Visual Studio 中运行会受到限制。