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

MFD*_*000 3 vb.net asp.net-mvc moq

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

Unable to cast object of type 'Moq.Mock`1[SRE.Web.Mvc.INotificationService]' to type 'SRE.Web.Mvc.INotificationService'.
Run Code Online (Sandbox Code Playgroud)

正如我所说,简单而且我不知道从哪里开始.

这是测试.

<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
Run Code Online (Sandbox Code Playgroud)

Jar*_*Par 6

该类型Mock(Of INotificationService)不可转换为INotificationServec因此您收到此错误.要获得实际的模拟对象,您需要使用该Object属性.

Dim controller As New CustomerServiceController(fakeNotifcationService.Object)
Run Code Online (Sandbox Code Playgroud)