我无法加载获取Module.psm1
到加载Module.tests.ps1
。
这是我的Module.psm1
。我添加了一个回车,Get-Function
只是看它是否会被测试跑步者捡起。
这是在Module.psm1
:
function Get-Function {
return $true
}
Module.psd1
:
FunctionsToExport = 'Get-Function'
Module.tests.ps1
:
Describe "Get-Function" {
Context "Function Exists" {
Import-Module .\Module.psm1
It "Should Return" {
Get-Function | Should Be $true
}
}
}
在针对模块编写测试之前,是否必须先将模块构建并加载到模块路径中?还是有一种方法可以引用其相对于测试路径的位置?
我在“输出”窗口中看到的结果是:
------ Run test started ------
Describing Get-Function
Context Function Exists
[-] Should Return 731ms
The term 'Get-Function' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
at line: 12 in C:\Users\Adam\Documents\code\Powershell\TestModuleProject\TestModuleProject\Module.tests.ps1
Tests completed in 731ms
Passed: 0 Failed: 1 Skipped: 0 Pending: 0
========== Run test finished: 1 run (0:00:08.1834099) ==========
有什么建议?
我怀疑工作目录(.
)与运行测试时文件所在的目录不同。您可以$MyInvocation.MyCommand.Path
用来确定测试脚本的目录。
Pester单元测试(这是您的测试代码和输出的样子)通常为此包含以下行:
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
尝试如下更改测试代码:
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
Describe "Get-Function" {
Context "Function Exists" {
Import-Module "$here\Module.psm1"
It "Should Return" {
Get-Function | Should Be $true
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句