chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi, I'm confused. I've watched the video on mocking DB calls and have succesfully mocked a "FindById()" DB function in my data access layer.

But what I really want to do is test code within the FindById() function, not mock the call to the method.
Here is the FindById() function:
  Public Function FindById(ByVal artistId As Long, ByVal recDataSet As RecordingDataSet) As RecordingDataSet.Artist
    Me.command.Parameters("@id").Value = artistId
    Me.adapter.Fill(recDataSet, recDataSet.Artists.TableName)
    Dim rows() As DataRow = recDataSet.Artists.Select(String.Format("id={0}", artistId))
    If rows.Length < 1 Then Return Nothing
    Return CType(rows(0), RecordingDataSet.Artist)
  End Function


The command and adapter are private variables at the class level.

My question is this - how do I set up the test code so I can test the last two lines of the FindById function, namely:
If rows.Length < 1 Then Return Nothing
Return CType(rows(0), RecordingDataSet.Artist)


TIA.
asked by M2r4Miller (600 points)

1 Answer

0 votes
Hi
Here's my suggestion:
Refactor the code that returns DataRow into a separate method:
Public Function GetDataRow(ByVal artistId As Long, ByVal recDataSet As RecordingDataSet) As DataRow()
  Me.command.Parameters("@id").Value = artistId
    Me.adapter.Fill(recDataSet, recDataSet.Artists.TableName)
    Dim rows() As DataRow = recDataSet.Artists.Select(String.Format("id={0}", artistId)) 
   Return rows
End Function


This will let you fake easily just what you need which is the DataSet object.
Than your test will look something like this:
    <TestMethod> Public Sub FindById_Test()
        Dim mockDataRow As RecorderManager.CreateObject(Of DataRow)
        Dim ret() As DataRow = {mockDataRow.Object}

        Using recorder As New RecordExpectations
            Dim mock As New ClassUnderTest()
            recorder.ExpectAndReturn(mock.GetDataRow, ret)
        End Using

        Dim classUnderTest As New ClassUnderTest()
        classUnderTest.FindById(1, New RecordingDataSet())
    End Sub


And just to taste the future - The next version of Typemock Isolator that will be out next week
will include special API for VB. Here's how the test will look like in the next version:
<TestMethod> Public Sub FindById_Test2()
   'Create our return value
   Dim fake As DataRow = FakeInstance(Of DataRow)()
   Dim ret() As DataRow = {fake}

   Dim classUnderTest As New SomeNameSpace.ClassUnderTest()
   ' Stubbing the GetDataRow method. When called the method will return the array we created
   Using TheseCalls.WillReturn(ret)
      classUnderTest.GetDataRow()
   End Using

   Dim artist As RecordingDataSet.Artist = classUnderTest.FindById(1, New SomeNameSpace.RecordingDataSet())
   'Asserting that FindById works as expected
   Assert.IsNotNull(artist)
End Sub


Hope it helps :)
answered by ohad (35.4k points)
...