Skip to content

TMock<IMyInterface>.MyMethod(X) leads to access violation when X is a record type #125

@alydouglas

Description

@alydouglas

I have started using Delphi-Mocks to help unit test a development that makes heavy use of records as input parameters.

I created a TMock of one of the interfaces and set it up using the Setup.WillReturn(Y).When.MyMethod(X) syntax where X in this case is a record type. However, when MyMethod is then called with X on the mock, it leads to an access violation in the Delphi.Mocks.Helpers.TValueHelper.

On investigation, it is caused when the logic in SameValue triggers AsPointer which in turn triggers Systtem.Rtti.TValue.ExtractRawDataNoCopy and then System.Rti.TValueDataImpl.ExtractRawDataNoCopy. This leads to an access violation as more than the size of a pointer is moved when the underlying type is a record.

As a modification to the TValueHelper I added a step to check whether the type is of kind tkRecord but this only helps identify that it is a record, so in addition, also added a mechanism for custom comparers to be registered by the user for specific record types which are then used to check if the two records are equal.

This is a similar mechanism to Generics.Collections where an IComparer can be defined for lists using record types but not ideal, however, seems to be the safest methodology to ensure a good comparison is made.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions