MFD3000
MFD3000

Reputation: 874

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

Upvotes: 4

Views: 1272

Answers (2)

Simon Steele
Simon Steele

Reputation: 11598

I think you need fakeNotifcationService.Object.

Upvotes: 2

JaredPar
JaredPar

Reputation: 755557

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)

Upvotes: 7

Related Questions