Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 具有私有构造函数的类的部分模拟_.net_Vb.net_Unit Testing_Moq_Mstest - Fatal编程技术网

.net 具有私有构造函数的类的部分模拟

.net 具有私有构造函数的类的部分模拟,.net,vb.net,unit-testing,moq,mstest,.net,Vb.net,Unit Testing,Moq,Mstest,我试图在一个类上编写一些单元测试,使用Moq: Public Interface IAwesomeInterface Function GetThis() As Integer Function GetThisAndThat(ByVal that As Integer) As Integer End Interface Public Class MyAwesomeClass Implements IAwesomeInterface Dim _this As

我试图在一个类上编写一些单元测试,使用Moq:

Public Interface IAwesomeInterface
    Function GetThis() As Integer
    Function GetThisAndThat(ByVal that As Integer) As Integer
End Interface


Public Class MyAwesomeClass
    Implements IAwesomeInterface

    Dim _this As Integer

    ''' <summary>
    ''' injection constructor
    ''' </summary>
    Private Sub New(ByVal this As Integer)
        Me._this = this
    End Sub

    ''' <summary>
    ''' default factory method
    ''' </summary>
    Public Shared Function Create() As IAwesomeInterface
        Return New MyAwesomeClass(42)
    End Function

    Public Overridable Function GetThis() As Integer Implements IAwesomeInterface.GetThis
        Return _this
    End Function

    Public Function GetThisAndThat(ByVal that As Integer) As Integer Implements IAwesomeInterface.GetThisAndThat
        Return GetThis() + that
    End Function
End Class
。。。但这失败了。执行测试时,GetThis返回56而不是99

我做错什么了吗? 在我读到的其他问题中,我没有看到提到这种情况

更新:基于Tim Long的回答

我将其添加到我正在测试的程序集的
AssemblyInfo.vb

<Assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")> 

您可以将构造函数设置为内部而不是私有,然后使用
InternalsVisibleTo
属性将单元测试指定为“友元程序集”


或者,可能值得一看(MS Research)

我曾尝试将构造器标记为
内部
(即
朋友
),并根据Moq的文档设置内部可视性(即带有
公钥的DynamicProxy Assembly2)它失败了,错误为System.NotSupportedException:Parent没有默认构造函数。必须明确定义默认构造函数。
。但不包括你说的公钥密码。。。谢谢你!
<Assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")> 
<TestClass()>
Public Class MyAwesomeTest

    <TestMethod()>
    Public Sub GetThisAndThat_calls_GetThis()
        'Arrange
        Dim dummyAwesome = New Mock(Of MyAwesomeClass)(56)
        dummyAwesome.CallBase = True

        dummyAwesome.Setup(Function(c) c.GetThis()).Returns(99)

        'Act
        Dim thisAndThat = dummyAwesome.Object.GetThisAndThat(1)

        'Assert
        Assert.AreEqual(100, thisAndThat)

        dummyAwesome.Verify(Function(d) d.GetThis, Times.Once, "GetThisAndThat should call GetThis")

    End Sub

End Class