我正在编写一个简单的单元测试,以检查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
答案 0 :(得分:5)
类型Mock(Of INotificationService)
无法转换为INotificationServec
,因此您收到此错误。要获取实际的模拟对象,您需要使用Object
属性。
Dim controller As New CustomerServiceController(fakeNotifcationService.Object)
答案 1 :(得分:2)
我认为你需要fakeNotifcationService.Object
。