我在 Pester 中有以下简单的测试:
# Name.Tests.ps1
$name = "foo"
Describe "Check name" {
It "should have the correct value" {
$name | Should -Be "foo"
}
}
因此,当我导航到包含测试脚本的文件夹并运行时Invoke-Pester
,我期待测试能够通过。相反,我收到以下错误:
[-]Check name.should have the correct value. Expected 'foo', but got $null...
知道为什么会失败以及为什么$name
中设置为 nullIt
阻止 - 不应该$name
仍设置为foo
因为它来自父范围?
Pester v5 有需要遵循的新规则,其中之一如下(https://github.com/pester/Pester#discovery--run https://github.com/pester/Pester#discovery--run):
将所有代码放入 It、BeforeAll、BeforeEach、AfterAll 或
每个之后。请勿将代码直接放入Describe、Context 或顶部
您的文件,而不将其包装在这些块之一中,除非您
有充分的理由这样做。
...
所有放错位置的代码都将在 Discovery 期间运行,并且其结果不会
在运行期间可用。
因此将变量赋值放在BeforeAll
or BeforeEach
内Describe
块应该让它工作:
Describe "Check name" {
BeforeAll {
$name = "foo"
}
It "should have the correct value" {
$name | Should -Be "foo"
}
}
感谢@Mark Wragg 为我指明了正确的方向!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)