在没有构造函数的情况下模拟对象

时间:2018-02-15 15:41:20

标签: powershell powershell-v4.0 business-objects-sdk

我正在尝试使用相同类型的对象测试具有强类型参数的函数。不幸的是,这个对象是由工厂创建的,没有构造函数。工厂需要服务器访问和身份验证才能创建它。强类型参数是此函数处理的必要条件。

有没有办法创建一个不需要工厂的模拟对象,但仍然会被解释为所需的类型(CrystalDecisions.Enterprise.Desktop.FavoritesFolder)?

Invoke-Foo.ps1

function Invoke-Foo {

    params(
    [CrystalDecisions.Enterprise.Desktop.FavoritesFolder]$folder
    )

    return $folder.Title

}

Invoke-Foo.Tests.ps1

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
. "$here\$sut"

Add-Type -AssemblyName 'CrystalDecisions.Enterprise.Desktop.FavoritesFolder, Version=14.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304'

Describe "Invoke-Foo" {

    # arrange
    $expected = 'Foobar'

    # error generated here
    $folder = New-Object CrystalDecisions.Enterprise.Desktop.FavoritesFolder
    $folder.Title = $expected

    # act
    $actual = Invoke-Foo $folder

    It "returns the name of the folder" {

        # assert
        $actual | should Be $expected

    }

}

测试失败:

PS> Invoke-Pester
Describing Invoke-Foo
 [-] Error occurred in Describe block 62ms
   PSArgumentException: A constructor was not found. Cannot find an appropriate constructor for type CrystalDecisions.Enterprise.Desktop.FavoritesFolder.
   at <ScriptBlock>, C:\Users\XXX\Desktop\Foo\Invoke-Foo.Tests.ps1: line 12
Tests completed in 62ms
Passed: 0 Failed: 1 Skipped: 0 Pending: 0 Inconclusive: 0

0 个答案:

没有答案