How to Mock OleDbDataAdapter(query, connStr)

Pat Zidane :

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
Nkosi :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=29018&siteId=1