Note that there are some explanatory texts on larger screens.

plurals
  1. POvb.net mocking dal methods with moq
    primarykey
    data
    text
    <p>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. </p> <p>The method to test:</p> <pre><code>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 &lt;= 0 Then msg = "There were no records found for the selected criteria." ElseIf _dt.Rows.Count + 1 &gt; 65536 Then msg = "Too many rows - Export to Excel not possible." Else rs.Export(_dt, "AcceptanceOfOffer", response) End If Return msg End Function </code></pre> <p>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. </p> <p>Here's my test, no nunit or anything like that:</p> <pre><code>'''&lt;summary&gt; '''A test for GetReport '''&lt;/summary&gt; &lt;TestMethod()&gt; _ 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 </code></pre> <p>Just in case it's relevant, the DAL class and method I am trying to mock.</p> <pre><code>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 </code></pre> <p>Obviously this doesn't work, I've checked the moq quickstart and other <a href="http://www.codethinked.com/beginning-mocking-with-moq-3-part-3" rel="nofollow noreferrer">places</a> without success. Is this even possible or should I be using .verify or something else? <a href="https://stackoverflow.com/questions/2180363/mstest-with-moq-dal-setup">This post</a> has the structure I want to use except in that case the mocked object is passed as an argument to the method.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload