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
0 votes
I have found a strange one that used to work in 3.7.1, but seems to be broken in 4.0.3. The following code will now fail with the error "Mocked return value of DataTableCollection.get_Item() is unknown, use recorder.Return().":

Public Class TheClass
    Public Function TheMethod(ByVal theDataTable As DataTable) As Integer
        Return theDataTable.Rows.Count
    End Function
End Class

Imports TypeMock
Imports MbUnit.Framework

<TestFixture()> _
Public Class TheTestClass

    <Test()> _
    Public Sub TheTestMethod()
        MockManager.Init()

        Dim ds As New DataSet
        ds.Tables.Add("aTable")
        Dim tc As New TheClass

        Using recorder As New RecordExpectations
            recorder.ExpectAndReturn(tc.TheMethod(ds.Tables(0)), 0)
        End Using

        Dim answer As Integer = tc.TheMethod(ds.Tables(0))

        MockManager.Verify()
    End Sub

End Class


Changing
recorder.ExpectAndReturn(tc.TheMethod(ds.Tables(0)), 0)

To
tc.TheMethod(ds.Tables(0))
recorder.Return(0)

Gives the same error message.

However, this works:
Dim dt As DataTable = ds.Tables(0)
Using recorder As New RecordExpectations
    recorder.ExpectAndReturn(tc.TheMethod(dt), 0)
End Using


Why does this no longer work (or am I doing something wrong (it happens :wink: ))?
asked by halstein (8.7k points)

3 Answers

0 votes
Hi
The reason for the changed behavior is the Intelligent Recording
which we added in version 4.0

Since you don't want to mock ds.Tables you should not sent it as an argument
inside the recording block.
Try:
Using recorder As New RecordExpectations
   recorder.ExpectAndReturn(tc.TheMethod(Nothing), 0)
End Using
answered by ohad (35.4k points)
0 votes
Actually the problem is that TypeMock doesn't know what to return when ds.Tables(0) is called. This call is mocked as it is in the using block.

If you don't want to mock this method use the following:
Using recorder As New RecordExpectations
    recorder.ExpectAndReturn(tc.TheMethod(Nothing), 0)
End Using 

If you want to mock this method, decide what you want the Tables() to return, you can use ReturnDefaultImplementation.
Using recorder As New RecordExpectations
    Dim item as String = ds.Tables(0)
    recorder.ReturnDefaultImplementation()
    recorder.ExpectAndReturn(tc.TheMethod(item), 0)
End Using 
answered by scott (32k points)
0 votes
Now I know that the change in behaviour was intended and not caused by a bug, and how to work with the new behaviour.

Thanks!
answered by halstein (8.7k points)
...