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 am trying to make use of the WasCalledWithArguments function but am having problems with the Matching statement. Below is my code:

Isolate.Verify.WasCalledWithArguments(Function() _fakeDbContext.OptionEntities.Add(Nothing)).Matching(Function(args) TypeOf args(0) Is OptionEntity)

When debugging, the "args" is empty and I get an index out of range exception trying to access args(0). It seems to not be picking up the argument passed into the Add method. What am I doing wrong?

asked by donniedarko (3.8k points)

1 Answer

0 votes


The verify call on it's own seems fine.

The methods used in verify must be faked, did you fake _fakeDbContext.OptionEntities.Add?

Note that you should use whenCalled(...).CallOriginal if you want the original behavior of the method.

See example:

Public Class AClass

    Public Sub DoWorkWithParams(num As Integer)

    End Sub

End Class

Public Sub verifyCalledWithParams_CheckType()

    Dim a As New AClass
    Isolate.WhenCalled(Sub() a.DoWorkWithParams(Nothing)).CallOriginal()


    Isolate.Verify.WasCalledWithArguments _
    (Sub() a.DoWorkWithParams(Nothing)) _
                    .Matching(Function(args) TypeOf (args(0)) Is Integer)

End Sub

Let me know if it helps.

answered by alex (17k points)
Ok thanks, I wasn't aware I had to fake the method in the first place in order to get this to work. I will give it a shot, thanks!!
    Public Sub AddOption()

        ' Arrange
        Dim criteria As New OptionCriteria

        criteria.OptionKey = OptionKeys.AssessorTabLabel
        criteria.OptionValue = "ASSESSOR"

        ' So that we can verify the method call with the proper criteria
        Isolate.WhenCalled(Function() _fakeDbContext.OptionEntities.Add(Nothing)).CallOriginal()

        ' Act 

        ' Assert/Verify
        Dim optionEntity As New OptionEntity

        Isolate.Verify.WasCalledWithArguments(Function() _fakeDbContext.OptionEntities.Add(Nothing)).Matching(Function(args) TypeOf args(0) Is OptionEntity)

    End Sub

I tried and got the same exception...


Method being tested. "MyDataContext" is being mocked.

Public Sub AddOption(ByVal criteria As OptionCriteria)

            Using dbContext As New MyDataContext(_connectionString)

                Dim optionEntity As New OptionEntity

                If criteria.UserId.HasValue Then

                    optionEntity.UserId = criteria.UserId.Value

                End If

                If Not String.IsNullOrWhiteSpace(optionEntity.Workstation) Then

                    optionEntity.Workstation = criteria.WorkstationName

                End If

                optionEntity.OptionKey = criteria.OptionKey
                optionEntity.OptionValue = criteria.OptionValue


            End Using

        End Sub



Some Questions

1. How are you faking _fakeDbContext?

2. How is OptionEntities defined?

3. How is Add defined?

Note: if the Add is part of mscorlib (List for example) then the best way to test this is to actually grab the list and check for the result.


_fakeDbContext.OptionEntities = New List(Of OptionEntities)

Dim optionEntity = _fakeDbContext.OptionEntities(0)


1) I am faking the _fakeDbContext like this:

     _fakeDbContext = Isolate.Fake.AllInstances(Of MyDataContext)

2) OptionEntities is defined as such in the unit test method:

     Dim optionEntities as New List(Of OptionEntity)

dbContext.OptionEntities is defined like this:

    Public Property OptionEntities as DbSet(Of OptionEntity)

3) Add is just off of a list as you mentioned.

I have tried to check the result but once executed the "OptionEntities" in my unit test method is still empty.