我想知道你是否可以帮助我理解一个moq概念......我有一个我想测试的方法。它包含我想要模拟的数据访问方法。
测试方法:
Public Function GetReport(ByVal district As String, ByVal hub As String, ByVal dateFrom As Date, ByVal dateTo As Date, ByVal response As HttpResponse) As String
Dim msg As String = String.Empty
Dim rs As New ReportingService
_dt = _dal.GetData(district, hub, dateFrom, dateTo)
If _dt.Rows.Count <= 0 Then
msg = "There were no records found for the selected criteria."
ElseIf _dt.Rows.Count + 1 > 65536 Then
msg = "Too many rows - Export to Excel not possible."
Else
rs.Export(_dt, "AcceptanceOfOffer", response)
End If
Return msg
End Function
我想测试控制逻辑。如果数据表有0,1行或多行,则应返回不同的消息。我不关心_dal.GetData的结果,这是我希望模拟的方法。
这是我的测试,没有nunit或类似的东西:
'''<summary>
'''A test for GetReport
'''</summary>
<TestMethod()> _
Public Sub GetReportTest()
'Create a fake object
Dim mock = New Mock(Of IAcceptanceOfferDAL)
'Create the real data to be returned by the fake
Dim returnDt As DataTable = New DataTable()
returnDt.Columns.Add("District", Type.GetType("System.String"))
returnDt.Columns.Add("Hub", Type.GetType("System.String"))
returnDt.Columns.Add("dateFrom", Type.GetType("System.DateTime"))
returnDt.Columns.Add("dateTo", Type.GetType("System.DateTime"))
returnDt.Rows.Add("District", "Hub", Date.Today, Date.Today)
'Setup the fake so that when the method is called the data created above will be returned
mock.Setup(Function(f) f.GetData(It.IsAny(Of String), It.IsAny(Of String), It.IsAny(Of Date), It.IsAny(Of Date))).Returns(returnDt)
'Call the real method with the expectation that when it calls GetData it will use our mock object
Dim target = New AcceptanceOfferBLL
Dim response As HttpResponse
Dim actual = target.GetReport("district", "hub", Date.Today, Date.Today, response)
'Because our mock returns 1 row it will skip over our if statements and should return string.empty
Assert.AreEqual("", actual)
End Sub
以防它是相关的,我试图模拟的DAL类和方法。
Public Interface IAcceptanceOfferDAL
Function GetData(ByVal district As String, ByVal site As String, ByVal dateFrom As Date, ByVal dateTo As Date) As DataTable
End Interface
Public Class AcceptanceOfferDAL : Implements IAcceptanceOfferDAL
Private _ds As New DataService.DataAccess
Private _sNameSP As String = ""
Private _listSQLParams As New List(Of SqlParameter)
Public Function GetData(ByVal district As String, ByVal site As String, ByVal dateFrom As Date, ByVal dateTo As Date) As DataTable Implements IAcceptanceOfferDAL.GetData
_sNameSP = "up_AcceptanceHub_get"
Dim sqlParam As SqlParameter = New SqlParameter("@district", district)
Dim sqlParam1 As SqlParameter = New SqlParameter("@hub", site)
Dim sqlParam2 As SqlParameter = New SqlParameter("@DateFrom", dateFrom)
Dim sqlParam3 As SqlParameter = New SqlParameter("@DateTo", dateTo)
_listSQLParams.Add(sqlParam)
_listSQLParams.Add(sqlParam1)
_listSQLParams.Add(sqlParam2)
_listSQLParams.Add(sqlParam3)
Return (_ds.LoadDataTableByID(_listSQLParams, _sNameSP))
End Function
End Class
显然这不起作用,我已经检查了moq快速启动和其他places但没有成功。这甚至是可能的还是我应该使用.verify或其他什么? This post具有我想要使用的结构,除非在这种情况下,模拟对象作为参数传递给方法。
答案 0 :(得分:0)
GetReport
方法取决于_dal
,GetReport
方法是在IAcceptanceOfferDAL
方法之外定义的。
因此,尽管为GetReport
创建了一个模拟对象,但该模拟对象不起作用,因为_dal
只知道使用在其他地方实例化的_dal
对象。
要解决此依赖关系,需要将Public Function GetReport(ByVal district As String
, ByVal hub As String
, ByVal dateFrom As Date
, ByVal dateTo As Date
, ByVal response As HttpResponse
, ByVal _dal As IAcceptanceOfferDAL) As String
对象作为参数传递给方法。
IAcceptanceOfferDAL
通过这样做,GetData
的模拟及其GetReport
函数的设置将在测试Dim actual = target.GetReport("district"
, "hub"
, Date.Today
, Date.Today
, response
, mock)
方法时起作用:
GetReport
因此,要明确的是,更改IAcceptanceOfferDAL
方法以使其接受GetData
的实例作为参数允许在测试时将模拟对象传递给此方法,并且能够通过当然,在那个模拟中,提供了对{{1}}方法的返回值的所需控制。
希望这有帮助