Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6718641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:59:41+00:00 2026-05-26T08:59:41+00:00

I was wondering if you can help me understand a moq concept… I have

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:59:41+00:00Added an answer on May 26, 2026 at 8:59 am

    The GetReport method is dependent on _dal, which is defined outside of the GetReport method.

    Thus, despite creating a mock object for IAcceptanceOfferDAL, that mock object doesn’t come into play because GetReport only knows to use the _dal object that was instantiated elsewhere.

    To get around this dependency, the _dal object needs to be passed into the method as a parameter.

    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
    

    By doing this, the mock of IAcceptanceOfferDAL and the setup for its GetData function will be in play when testing the GetReport method like so:

    Dim actual =  target.GetReport("district"
                                 , "hub"
                                 , Date.Today
                                 , Date.Today
                                 , response
                                 , mock)
    

    So, to be clear, changing the GetReport method such that it accepts an instance of IAcceptanceOfferDAL as a parameter allows a mock object to be passed into this method when testing, and the ability to pass in that mock, of course, provides the desired control over the return value of the GetData method.

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering if someone can help me understand the following behaviour. If I have
I was wondering if someone can help me out with this issue. I have
Wondering if any of you can help me: I've made a signup modal that
Wondering if there is any tool that can help me to detect a pronoun's
I understand that Java can load/execute DLL code, but I'm wondering if there are
Im wondering if someone can help me understand the best way for me to
Wondering if anyone can help with this. I have a table with some fixed
I'm wondering if anybody can help? I have a large number of excel files.
I am wondering if someone could help me understand how I can achieve this.
Just wondering if someone can help me understand why my regular expression is matching

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.