2

How can i mock OleDbDataAdapter(query, conn) in FilledDataInDataTable function? Can i? I have no idea how i do with dependency from query and connection string parameters.

Public Class DataTableOfDataFromExportCSV

   Private _adp As IOleDbDataAdapter

   Public Sub New(iadp As IOleDbDataAdapter)
       _adp = iadp
   End Sub

   Public Function FilledDataInDataTable(query As String, conStr As String) As DataTable

     Dim dt As New DataTable
     Dim adp = _adp.OleDbDataAdapter(query, conStr)
     adp.Fill(dt)

     Return dt

  End Function

End Class

Implementation

Public Class MyOleDbDataAdapter
 Implements IOleDbDataAdapter

 Public Function OleDbDataAdapter(query As String, conn As String) As OleDbDataAdapter Implements IOleDbDataAdapter.OleDbDataAdapter
    Dim adp As New OleDbDataAdapter(query, conn)
    Return adp

 End Function
End Class

My Interface

Public Interface IOleDbDataAdapter
   Function OleDbDataAdapter(query As String, conn As String) As OleDbDataAdapter
End Interface
Pat Zidane
  • 23
  • 4
  • 2
    Seems to me that the OleDbDataAdapter would work best as a tightly-coupled implementation detail of a slightly higher-level interface/object representing a more general data store. Then, you wouldn't be mocking the OleDbDataAdapter, you'd be mocking your higher-level data store. YMMV, I haven't done much with abstracting over data storage. – Craig Mar 06 '20 at 15:46
  • Thank you @Craig :) – Pat Zidane Mar 06 '20 at 18:21

1 Answers1

3

How can I mock OleDbDataAdapter

You can't because it is NotInheritable (sealed - c#).

Public NotInheritable Class OleDbDataAdapter
Inherits DbDataAdapter
Implements ICloneable

What we have here is a Leaky Abstraction.

Expose only what is explicitly needed to perform the desired function.

For example

Public Interface IOleDbDataAdapter
   Function Fill(query As String, dataTable As DataTable) As Integer
End Interface

That way, implementation details/concerns like OleDbDataAdapter wont cause tight coupling.

Public Class DataTableOfDataFromExportCSV

    Private adp As IOleDbDataAdapter

    Public Sub New(iadp As IOleDbDataAdapter)
        adp = iadp
    End Sub

    Public Function FilledDataInDataTable(query As String) As DataTable
        Dim dt As New DataTable
        adp.Fill(query, dt)
        Return dt
    End Function

End Class

Note the removal of runtime data that can be managed at the composition root when constructing your components' object graphs or after the component has already been constructed.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • 1
    @PatZidane no worries, Glad to help. This is more of a design issue. The unit test only exposed the design issue at hand. – Nkosi Mar 06 '20 at 17:02
  • Thank you very much @Nkosi, I am still new in Unit test and programming. I couldn't understand something from you're explain. I have to learn something more about your answer. Great!! Now I got it that I can not Mock OleDbDataAdapter. :) – Pat Zidane Mar 06 '20 at 17:11