为什么不起订量运行重写的ToString方法? [英] Why doesn't Moq run the overridden ToString method?

查看:173
本文介绍了为什么不起订量运行重写的ToString方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下code为什么mockTest.ToString()返回NULL?

编辑:添加注释到例如code来说明如何解决这个问题

 公用Sub主要()

    尝试

        昏暗的测试=新的TestClass

        如果test.ToString<> 计算器规则然后
            抛出新的异常(真失败:实际值:其中,+ test.ToString +>中)
        结束如果

        昏暗的模拟=新Moq.Mock(中识别TestClass)()
        mock.SetupGet(功能(M作为识别TestClass)m.Name).Returns(模拟值)

        根据马克的接受的答案,这是缺少的行
        的code,使code的工作。
        mock.CallBase = TRUE

        昏暗mockTest = DirectCast(mock.Object,TestClass中)

        如果mockTest.ToString()&其中;> 模拟值然后
            抛出新的异常(模拟失败:实际值:其中,+ mockTest.ToString +>中)
        结束如果

        Console.WriteLine(通过所有的测试。)

    抓住EX为例外

        Console.ForegroundColor = ConsoleColor.Red
        Console.WriteLine(ex.ToString)
        Console.ForegroundColor = ConsoleColor.White

    结束尝试

    Console.WriteLine()
    Console.WriteLine(完了!)
    Console.ReadKey()

结束小组

公共类识别TestClass

    公共子新()
    结束小组

    公众可重写只读属性名称()作为字符串
        得到
            返回计算器规则
        最终获取
    高端物业

    公共重写功能的ToString()作为字符串
        返回Me.Name
    端功能

末级
 

解决方案

无论是Name属性和TestClass的ToString方法是虚拟/重写,这意味着,起订量会嘲笑他们。

在默认情况下,起订量返回成员引用类型返回类型为空,除非你明确告诉它返回别的东西。既然字符串是引用类型,则返回null。

您可以通过设置Call​​Base到真正的解决这个问题。

设置Call​​Base为true,将导致起订量调用基实现,如果你不显式定义一个覆盖:

  mock.CallBase = TRUE
 

在这种情况下,这将指示模拟,因为没有eplicit设置存在使用的ToString的基实现(因此调用名称属性,它的确实的有一个设置)。

In the following code why does mockTest.ToString() return Null?

EDIT: Added comment into example code to show how to fix the problem.

Public Sub Main()

    Try

        Dim test = New TestClass

        If test.ToString <> "stackoverflow rules" Then
            Throw New Exception("Real Failed: Actual value: <" + test.ToString + ">")
        End If

        Dim mock = New Moq.Mock(Of TestClass)()
        mock.SetupGet(Function(m As TestClass) m.Name).Returns("mock value")

        ' As per Mark's accepted answer this is the missing line of 
        ' of code to make the code work.
        ' mock.CallBase = True

        Dim mockTest = DirectCast(mock.Object, TestClass)

        If mockTest.ToString() <> "mock value" Then
            Throw New Exception("Mock Failed: Actual value: <" + mockTest.ToString + ">")
        End If

        Console.WriteLine("All tests passed.")

    Catch ex As Exception

        Console.ForegroundColor = ConsoleColor.Red
        Console.WriteLine(ex.ToString)
        Console.ForegroundColor = ConsoleColor.White

    End Try

    Console.WriteLine()
    Console.WriteLine("Finished!")
    Console.ReadKey()

End Sub

Public Class TestClass

    Public Sub New()
    End Sub

    Public Overridable ReadOnly Property Name() As String
        Get
            Return "stackoverflow rules"
        End Get
    End Property

    Public Overrides Function ToString() As String
        Return Me.Name
    End Function

End Class

解决方案

Both the Name property and the ToString method on TestClass are virtual/overridable, which means that Moq will mock them.

By default, Moq returns null for members with reference type return types, unless you explicitly tell it to return something else. Since a string is a reference type, it returns null.

You can fix it by setting CallBase to true.

Setting CallBase to true will cause Moq to call the base implementation if you do not explictly define an override:

mock.CallBase = True

In this case, this will instruct the mock to use the base implementation of ToString since no eplicit Setup exists (and thus invoke the Name property, which does have a Setup).

这篇关于为什么不起订量运行重写的ToString方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆