apex-enterprise-patterns / fflib-apex-common

Common Apex Library supporting Apex Enterprise Patterns and much more!
BSD 3-Clause "New" or "Revised" License
911 stars 517 forks source link

Verify on mocked UoW.registerDirty() with relationship fails with matcher number mismatch #416

Open janskola opened 2 years ago

janskola commented 2 years ago

Issue description When I need to verify fflib_ISObjectUnitOfWork.registerDirty(SObject, SObjectField, SObject) method call on a mocked instance, I'm getting this exception: fflib_ApexMocks.ApexMocksException: The number of matchers defined (3). does not match the number expected (1)

To Reproduce I've created classes to reproduce the issue.

Steps to reproduce the behavior:

  1. Create a scratch org with fflib-apex-common deployed
  2. Deploy following classes SimpleClass, SimpleClass_Test
  3. Run the test method SimpleClass_Test.testIssue()
  4. See error

SimpleClass.cls

public with sharing class SimpleClass {

    public void modifyData(Account a, Contact c, fflib_ISObjectUnitOfWork uow) {
        uow.registerDirty(c, Contact.AccountId, a);
    }
}

SimpleClass_Test.cls

@IsTest
private class SimpleClass_Test {
    @IsTest
    private static void testIssue() {
        fflib_ApexMocks mocks = new fflib_ApexMocks();
        fflib_ISObjectUnitOfWork uowMock = new fflib_SObjectMocks.SObjectUnitOfWork(mocks);

        Account a = new Account();
        Contact c = new Contact();

        SimpleClass sc = new SimpleClass();

        Test.startTest();
        sc.modifyData(a, c, uowMock);
        Test.stopTest();

        ((fflib_ISObjectUnitOfWork) mocks.verify(uowMock, mocks.times(1)))
            .registerDirty(
                fflib_Match.sObjectOfType(Contact.SObjectType),
                fflib_Match.anySObjectField(),
                fflib_Match.sObjectOfType(Account.SObjectType)
            );
    }
}

Expected behavior This test method is expected to execute without any issue and result in a test pass.

Error message observed

fflib_ApexMocks.ApexMocksException: The number of matchers defined (3). does not match the number expected (1)
If you are using matchers all arguments must be passed in as matchers.
For example myList.add(fflib_Match.anyInteger(), 'String') should be defined as myList.add(fflib_Match.anyInteger(), fflib_Match.eq('String')).
Class.fflib_Match: line 51, column 1
Class.fflib_AnyOrder.verify: line 23, column 1
Class.fflib_MethodVerifier.verifyMethodCall: line 20, column 1
Class.fflib_ApexMocks.verifyMethodCall: line 132, column 1
Class.fflib_ApexMocks: line 256, column 1
Class.fflib_ApexMocks: line 236, column 1
Class.fflib_SObjectMocks.SObjectUnitOfWork.registerDirty: line 145, column 1
Class.SimpleClass_Test.testIssue: line 20, column 1

Version Reproducible for me on latest version of fflib-apex-common.