我如何使用 Pester 模拟 .cs 文件的内容,让我遍历每一行和每个字符?
How can i mock the contents of a .cs file with Pester allowing me to iterate through each line and each character?
简短描述:
我正在尝试使用 Pester 来模拟文件的内容,然后遍历该假数据的每一行和每个字符,但我收到此错误:"the array index evaluated to null." 在尝试访问一行字符。
背景:
我有一个 Powershell 函数可以读取 global.asax.cs 文件的内容。
我正在读取的文件示例:
namespace MockData
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
MvcHandler.DisableMvcResponseHeader = true;
}
}
}
然后,我的函数将使用 Get-Content 读取文件并遍历每一行和每个字符。示例(简化版本,$index 变量在此示例中看起来很讨厌,但在实际代码中需要它):
$contents = Get-Content $filePath
$index = 0
$bracketCounter = 0
Do {
foreach($char in $contents[$index].ToCharArray()) {
if($char -eq "{") {
$bracketCounter++
}
elseif ($char -eq "}") {
$bracketCounter--
}
}
$index++
} While ($bracketCounter -gt 0)
现在在 Pester 中,这是我的测试代码:
It "check for success" {
Mock Get-Content {
$content = "namespace MockData
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
MvcHandler.DisableMvcResponseHeader = true;
}
}
}"
return $content
} -ParameterFilter { $Path -like "*asax*"}
[string[]]$errors = MyPowershellFile
$errors | should BeNullOrEmpty
}
预期结果:
我的预期结果是 Pester 测试以与实际内容相同的方式遍历模拟内容。
Pester 调用此行时发生错误:
foreach($char in $contents[$index].ToCharArray())
我得到"RuntimeException: Index operation failed; the array index evaluated to null."
我认为发生这种情况是因为在实际函数中,$contents 的第一个元素是文件的第一行。
$contents[0] = namespace MockData
而在 Pester 函数中,$contents 的第一个元素是第一个字符。
$contents[0] = n
有解决这个问题的好方法吗?我 运行 没主意了。
提前感谢阅读此问题的任何人。
Get-Content
returns 一个字符串数组,而你的模拟 returns 一个字符串。
试试下面的代码:
It "check for success" {
Mock Get-Content {
$content = @("namespace MockData"
"{"
" public class WebApiApplication : System.Web.HttpApplication"
" {"
" protected void Application_Start()"
" { "
" MvcHandler.DisableMvcResponseHeader = true;"
" }"
" }"
"}")
return $content
} -ParameterFilter { $Path -like "*asax*"}
[string[]]$errors = MyPowershellFile
$errors | should BeNullOrEmpty
}
为什么不写入 TestDrive 然后模拟:
Describe 'test things' {
#create a here-string and write the contents to the testdrive
@"
namespace MockData
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
MvcHandler.DisableMvcResponseHeader = true;
}
}
}
"@ | Set-Content -Path TestDrive:\testfile.txt
#Set up the mock to get-content
Mock Get-Content {
Get-Content TestDrive:\testfile.txt
} -ParameterFilter {$path -like "*asax*"}
#make sure the content matches
it "finds strings in mocked content" {
Get-Content abc.asax | Select-String MVCHandler | Should -Match 'MvcHandler.DisableMvcResponseHeader = true;'
}
#make sure the line count is correct
it 'should have 10 lines of content' {
(Get-Content def.asax).Count | Should -BeExactly 10
}
#make sure our mock was called the correct number of times
it "called mocked get-content twice" {
Assert-MockCalled Get-Content -Times 2
}
}
这将创建一个模拟生产的实际文件,以便您可以正确测试它。
简短描述:
我正在尝试使用 Pester 来模拟文件的内容,然后遍历该假数据的每一行和每个字符,但我收到此错误:"the array index evaluated to null." 在尝试访问一行字符。
背景:
我有一个 Powershell 函数可以读取 global.asax.cs 文件的内容。
我正在读取的文件示例:
namespace MockData
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
MvcHandler.DisableMvcResponseHeader = true;
}
}
}
然后,我的函数将使用 Get-Content 读取文件并遍历每一行和每个字符。示例(简化版本,$index 变量在此示例中看起来很讨厌,但在实际代码中需要它):
$contents = Get-Content $filePath
$index = 0
$bracketCounter = 0
Do {
foreach($char in $contents[$index].ToCharArray()) {
if($char -eq "{") {
$bracketCounter++
}
elseif ($char -eq "}") {
$bracketCounter--
}
}
$index++
} While ($bracketCounter -gt 0)
现在在 Pester 中,这是我的测试代码:
It "check for success" {
Mock Get-Content {
$content = "namespace MockData
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
MvcHandler.DisableMvcResponseHeader = true;
}
}
}"
return $content
} -ParameterFilter { $Path -like "*asax*"}
[string[]]$errors = MyPowershellFile
$errors | should BeNullOrEmpty
}
预期结果:
我的预期结果是 Pester 测试以与实际内容相同的方式遍历模拟内容。
Pester 调用此行时发生错误:
foreach($char in $contents[$index].ToCharArray())
我得到"RuntimeException: Index operation failed; the array index evaluated to null."
我认为发生这种情况是因为在实际函数中,$contents 的第一个元素是文件的第一行。
$contents[0] = namespace MockData
而在 Pester 函数中,$contents 的第一个元素是第一个字符。
$contents[0] = n
有解决这个问题的好方法吗?我 运行 没主意了。
提前感谢阅读此问题的任何人。
Get-Content
returns 一个字符串数组,而你的模拟 returns 一个字符串。
试试下面的代码:
It "check for success" {
Mock Get-Content {
$content = @("namespace MockData"
"{"
" public class WebApiApplication : System.Web.HttpApplication"
" {"
" protected void Application_Start()"
" { "
" MvcHandler.DisableMvcResponseHeader = true;"
" }"
" }"
"}")
return $content
} -ParameterFilter { $Path -like "*asax*"}
[string[]]$errors = MyPowershellFile
$errors | should BeNullOrEmpty
}
为什么不写入 TestDrive 然后模拟:
Describe 'test things' {
#create a here-string and write the contents to the testdrive
@"
namespace MockData
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
MvcHandler.DisableMvcResponseHeader = true;
}
}
}
"@ | Set-Content -Path TestDrive:\testfile.txt
#Set up the mock to get-content
Mock Get-Content {
Get-Content TestDrive:\testfile.txt
} -ParameterFilter {$path -like "*asax*"}
#make sure the content matches
it "finds strings in mocked content" {
Get-Content abc.asax | Select-String MVCHandler | Should -Match 'MvcHandler.DisableMvcResponseHeader = true;'
}
#make sure the line count is correct
it 'should have 10 lines of content' {
(Get-Content def.asax).Count | Should -BeExactly 10
}
#make sure our mock was called the correct number of times
it "called mocked get-content twice" {
Assert-MockCalled Get-Content -Times 2
}
}
这将创建一个模拟生产的实际文件,以便您可以正确测试它。