【发布时间】:2017-12-15 22:56:25
【问题描述】:
我创建了一个 PowerShell 测试脚本 Common.tests.ps1,使用 Pester 在同一目录中的 PowerShell 脚本 Common.ps1 中的一些函数。
在同一目录中还有一个TestInitializer.ps1 脚本,它使用Microsoft.Xrm.Data.PowerShell 模块在Dynamics CRM 实例中创建和获取记录。
从 Visual Studio 运行 PowerShell 测试脚本时,测试在测试资源管理器中失败并显示以下消息:
CommandNotFoundException:无法加载模块“Microsoft.Xrm.Data.PowerShell”。有关详细信息,请运行“Import-Module Microsoft.Xrm.Data.PowerShell”。
但是,从 PowerShell ISE 运行相同的测试时,运行没有问题。这似乎好像没有为 Visual Studio 运行的实例安装模块(我在运行 Get-Module -ListAvailable 并看到输出不包括用于 Visual Studio 测试的 Microsoft.Xrm.Data.PowerShell 模块时确认了这一点),不过即使像 Import-Module Microsoft.Xrm.Data.PowerShell -Global -Force 这样的显式调用似乎也不会在使用 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 中运行会受到限制。
【问题讨论】:
标签: powershell visual-studio-2015 pester