MOQ - 通过方法返回的返回值

时间:2017-10-24 13:36:21

标签: unit-testing mocking nunit moq ninject.web.mvc

我在界面上有一个方法:

string GetUserDetails(string whatever);

我想用MOQ模拟它,以便它返回方法返回的任何内容,即用户详细信息 - 类似于:

_mock.Setup( theObject => theObject.GetUserDetails( It.IsAny<string>( ) ) )
   .Returns( [object return by GetUserDetails method] ) ;

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

对于相关描述的情况,您需要使用部分模拟。在Moq,有两种不同的方法可以实现它:

  1. 在构造时指定CallBase:editNode。在这种情况下,默认情况下调用此对象方法将执行实际方法实现(如果有)。

  2. 为某些特定方法指定CallBase:Sub Module1() Dim Row As Integer Dim Counter As Integer Dim x As Integer Dim ColN As Integer Dim Col As Integer Dim RowN As Integer RowN = 1674 ColN = 2 Counter = 0 For x = 1 To 49 For Row = 3 To 1669 A = Worksheets("Sheet12").Cells(Row, 2).Value B = Worksheets("Sheet12").Cells(Row, 3).Value C = Worksheets("Sheet12").Cells(Row, 4).Value D = Worksheets("Sheet12").Cells(Row, 5).Value E = Worksheets("Sheet12").Cells(Row, 6).Value F = Worksheets("Sheet12").Cells(Row, 7).Value If A = x Or B = x Or C = x Or D = x Or E = x Or F = x Then Worksheets("Sheet12").Cells(RowN, ColN).Value = Counter ColN = ColN + 1 Counter = 0 Else Counter = Counter + 1 End If Next Row RowN = RowN + 1 ColN = 2 Next x End Sub

  3. 另见When mocking a class with Moq, how can I CallBase for just specific methods?

相关问题