PowerShell 中的 Should -Throw 方法
Should -Throw Method in PowerShell
我正在尝试 运行 在 PowerShell 中进行一些测试,以检查 SolutionInfo.cs 文件中的版本格式是否正确、是否丢失或是否正常。
通过我的测试,我试图涵盖这些场景。其中一个用于检查文件,当版本没问题时,通过了,但其余的都失败了。此外,我还向您发送了脚本和测试。解决方案文件包含版本。有人可以帮我解决这两种情况吗?throw
方法或任何其他适合我的情况的方法应该是什么样子?
function Get-VersionFromSolutionInfoFile($path) {
try {
[Version]$version = (Get-Content -Raw $path) -replace '(?s).*\bAssemblyVersion\("(.*?)"\).*', ''
}
catch {
throw "Missing version or incorrect format."
}
return $version.ToString()
}
it "returns version from SolutionInfo.cs file"{
Get-VersionFromSolutionInfoFile("$pwd/SolutionInfo.cs") | Should -Be '1.0.0.0'
}
it "returns exception if there aren't any versions in the file"{
Get-VersionFromSolutionInfoFile("$pwd/SolutionInfo2.cs") | Should -Throw
}
it "returns exception if the version is not in the correct format"{
Get-VersionFromSolutionInfoFile("$pwd/SolutionInfo3.cs") | Should -Throw
}
使用Should -Throw
时,您需要提供一个脚本块作为输入,因此您的测试需要如下:
it "returns exception if there aren't any versions in the file"{
{ Get-VersionFromSolutionInfoFile("$pwd/SolutionInfo2.cs") } | Should -Throw
}
it "returns exception if the version is not in the correct format"{
{ Get-VersionFromSolutionInfoFile("$pwd/SolutionInfo3.cs") } | Should -Throw
}
您还应该将 -ErrorAction Stop
添加到您的 Get-Content
。这确保您的 Catch
被触发,因为它强制异常终止:
function Get-VersionFromSolutionInfoFile($path) {
try {
[Version]$version = (Get-Content -Raw $path -ErrorAction Stop) -replace '(?s).*\bAssemblyVersion\("(.*?)"\).*', ''
}
catch {
throw "Missing version or incorrect format."
}
return $version.ToString()
}
我正在尝试 运行 在 PowerShell 中进行一些测试,以检查 SolutionInfo.cs 文件中的版本格式是否正确、是否丢失或是否正常。
通过我的测试,我试图涵盖这些场景。其中一个用于检查文件,当版本没问题时,通过了,但其余的都失败了。此外,我还向您发送了脚本和测试。解决方案文件包含版本。有人可以帮我解决这两种情况吗?throw
方法或任何其他适合我的情况的方法应该是什么样子?
function Get-VersionFromSolutionInfoFile($path) {
try {
[Version]$version = (Get-Content -Raw $path) -replace '(?s).*\bAssemblyVersion\("(.*?)"\).*', ''
}
catch {
throw "Missing version or incorrect format."
}
return $version.ToString()
}
it "returns version from SolutionInfo.cs file"{
Get-VersionFromSolutionInfoFile("$pwd/SolutionInfo.cs") | Should -Be '1.0.0.0'
}
it "returns exception if there aren't any versions in the file"{
Get-VersionFromSolutionInfoFile("$pwd/SolutionInfo2.cs") | Should -Throw
}
it "returns exception if the version is not in the correct format"{
Get-VersionFromSolutionInfoFile("$pwd/SolutionInfo3.cs") | Should -Throw
}
使用Should -Throw
时,您需要提供一个脚本块作为输入,因此您的测试需要如下:
it "returns exception if there aren't any versions in the file"{
{ Get-VersionFromSolutionInfoFile("$pwd/SolutionInfo2.cs") } | Should -Throw
}
it "returns exception if the version is not in the correct format"{
{ Get-VersionFromSolutionInfoFile("$pwd/SolutionInfo3.cs") } | Should -Throw
}
您还应该将 -ErrorAction Stop
添加到您的 Get-Content
。这确保您的 Catch
被触发,因为它强制异常终止:
function Get-VersionFromSolutionInfoFile($path) {
try {
[Version]$version = (Get-Content -Raw $path -ErrorAction Stop) -replace '(?s).*\bAssemblyVersion\("(.*?)"\).*', ''
}
catch {
throw "Missing version or incorrect format."
}
return $version.ToString()
}