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("fake@fake.com", "fakesubject", "fakemessage")
'Assert
Assert.AreEqual("thankyou", result.ViewName)
End Sub
The type
Mock(Of INotificationService)is not convertible toINotificationServechence you are getting this error. To get to the actual mock’d object you need to use theObjectproperty.