c# - 我如何使用 Pester 模拟 .cs 文件的内容,从而允许我迭代每一行和每个字符?

标签 c# powershell unit-testing pester

简短描述:

我尝试使用 Pester 模拟文件的内容,然后迭代该假数据的每一行和每个字符,但我收到此错误:“数组索引计算为 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:索引操作失败;数组索引计算为 null。”

我认为发生这种情况是因为在实际函数中, $contents 的第一个元素是文件的第一行。

$contents[0] = namespace MockData

在 Pester 函数中,$contents 的第一个元素是第一个字符。

$contents[0] = n

有什么好的方法可以解决这个问题吗?我的想法已经用完了。

预先感谢任何阅读此问题的人。

最佳答案

为什么不写信给 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
    }
}

这将创建一个模拟生产的实际文件,以便您可以正确测试它。

关于c# - 我如何使用 Pester 模拟 .cs 文件的内容,从而允许我迭代每一行和每个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56007439/

相关文章:

c# - EF核心: Using ID as Primary key and foreign key at same time

c# - Visual Studio 自动格式化没有正确格式化我的 Razor 代码

c# - 从另一个页面调用非静态方法

c# - 如何在 C# 中处理 Powershell Format-List 输出

c# - 如何使用 Marshal.getActiveObject() 获取正在运行的进程的 2 个实例,该实例有两个打开的进程

powershell - 英语非美国文化问题

windows - 我们如何在 Powershell 中的 HttpClient 对象上添加 DefaultRequestVersion?

c++ - 在 C++ 中使用友元类还是为单元测试添加访问器?

c# - 如何对依赖于 HttpContext.Current 和 Sitecore.Context 的类进行单元测试?

angular - 如何在 Angular 组件中测试 ViewChild/ContentChild 属性?