apex-enterprise-patterns / fflib-apex-mocks

An Apex mocking framework for true unit testing in Salesforce, with Stub API support
BSD 3-Clause "New" or "Revised" License
423 stars 214 forks source link

makeRelationship does not work with Junction Objects #122

Closed AllanOricil closed 3 years ago

AllanOricil commented 3 years ago

I have the following relationships

DocumentSigningRequestc 1 : N DocumentSigningRequestSignerc DocumentSigningRequestSigner__c 1 : 1 Contact

I used makeRelationship in the following way


...

DocumentSigningRequest__c documentSignignRequest = new DocumentSigningRequest__c(
                Status__c = 'New', 
                ParentId__c = parentId, 
                Id = fflib_IDGenerator.generate(Schema.DocumentSigningRequest__c.SObjectType)
        );

        Contact c = new Contact (
            Id = fflib_IDGenerator.generate(Schema.Contact.SObjectType), 
            FirstName = 'Test',
            LastName = 'Test',
            Email = 'test@test.com',
            Phone = '0000000000'
        );

        DocumentSigningRequestSigner__c documentSigningRequestSigner = new DocumentSigningRequestSigner__c(
            Id = fflib_IDGenerator.generate(Schema.DocumentSigningRequestSigner__c.SObjectType),
            Contact__c = c.Id,
            DocumentSigningRequest__c = documentSignignRequest.Id
        );

List<DocumentSigningRequestSigner__c> documentSigningRequestSigners = new List<DocumentSigningRequestSigner__c>
        {
            documentSigningRequestSigner
        };

        List<DocumentSigningRequest__c> documentSigningRequests = new List<DocumentSigningRequest__c>
        {
            documentSignignRequest
        };

List<DocumentSigningRequest__c> documentSigningRequestsWithSigners = (List<DocumentSigningRequest__c>) 
        fflib_ApexMocksUtils.makeRelationship(
            List<DocumentSigningRequest__c>.class,
            documentSigningRequests, 
            DocumentSigningRequestSigner__c.DocumentSigningRequest__c,
            new List<List<DocumentSigningRequestSigner__c>> { documentSigningRequestSigners });

        List<DocumentSigningRequestSigner__c> t = documentSigningRequestsWithSigners[0].DocumentSigningRequestSigners__r;
        System.assertNotEquals(null, t);
        System.assertEquals(1, t.size());
        System.debug('signer');
        System.debug(t[0]);
        System.debug('Contact First Name');
        System.debug(t[0].Contact__r.FirstName);

This is what it outputs

image

I have also tried to create a DocumentSigningRequestSigner__c using object references but it also did not work.

 DocumentSigningRequestSigner__c documentSigningRequestSigner = new DocumentSigningRequestSigner__c(
            Id = fflib_IDGenerator.generate(Schema.DocumentSigningRequestSigner__c.SObjectType),
            Contact__r = c,
            DocumentSigningRequest__r = documentSignignRequest
        );

So how can I make it return Contact__r.FirstName?

I need it because Im going to use this object in a selector mock

AllanOricil commented 3 years ago

Ok, I tried again doing this way

DocumentSigningRequestSigner__c documentSigningRequestSigner = new DocumentSigningRequestSigner__c(
            Id = fflib_IDGenerator.generate(Schema.DocumentSigningRequestSigner__c.SObjectType),
            Contact__r = c,
            DocumentSigningRequest__r = documentSignignRequest
        );

and it worked

image

I think when I tried it for the first time I may have forgotten something