I was wondering if you can help me understand a moq concept... I have a method I want to test. It contains a data access method that I want to mock.
The method to test:
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
I want to test the control logic. If the datatable has 0,1 or many rows a different message should be returned.. I don't care about the result of _dal.GetData, it's the method I am hoping to mock.
Here's my test, no nunit or anything like that:
'''<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
Just in case it's relevant, the DAL class and method I am trying to mock.
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
Obviously this doesn't work, I've checked the moq quickstart and other places without success. Is this even possible or should I be using .verify or something else? This post has the structure I want to use except in that case the mocked object is passed as an argument to the method.