Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MOQ: Cant cast Moq.Mock to Interface in VB.Net

I am writing a simple unit test to check if the proper view name is being returned by an ActionResult in VB.Net MVC 1. The controller requires a service and I am trying to Mock the service but keep getting this error.

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

As I said, the simple and I'm not sure where to go from here.

Here is the test.

<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("[email protected]", "fakesubject", "fakemessage")
        'Assert
        Assert.AreEqual("thankyou", result.ViewName)

    End Sub
like image 891
MFD3000 Avatar asked Sep 05 '25 03:09

MFD3000


1 Answers

The type Mock(Of INotificationService) is not convertible to INotificationServec hence you are getting this error. To get to the actual mock'd object you need to use the Object property.

Dim controller As New CustomerServiceController(fakeNotifcationService.Object)
like image 140
JaredPar Avatar answered Sep 09 '25 18:09

JaredPar