MOQ:不能将Moq.Mock转换为VB.Net中的接口

时间:2010-11-03 16:29:42

标签: asp.net-mvc vb.net moq

我正在编写一个简单的单元测试,以检查VB.Net MVC中的ActionResult是否返回了正确的视图名称。控制器需要一个服务,我正在尝试模拟服务,但一直收到此错误。< / p>

Unable to cast object of type 'Moq.Mock`1[SRE.Web.Mvc.INotificationService]' to type 'SRE.Web.Mvc.INotificationService'.

正如我所说的那样简单,我不知道从哪里开始。

这是测试。

<Test()> _
    Public Sub Index_Properly_Validates_Email_Address()
        'Arrange
        Dim fakeNotifcationService As New Mock(Of INotificationService)(MockBehavior.Strict)
        Dim controller As New CustomerServiceController(fakeNotifcationService)
        Dim result As ViewResult


        'Act
        result = controller.Feedback("fake@fake.com", "fakesubject", "fakemessage")
        'Assert
        Assert.AreEqual("thankyou", result.ViewName)

    End Sub

2 个答案:

答案 0 :(得分:5)

类型Mock(Of INotificationService)无法转换为INotificationServec,因此您收到此错误。要获取实际的模拟对象,您需要使用Object属性。

Dim controller As New CustomerServiceController(fakeNotifcationService.Object)

答案 1 :(得分:2)

我认为你需要fakeNotifcationService.Object