Skip to content

Calling fflib_ApexMocksUtils.setReadOnlyFields() on record with children leads to Exception #163

@Seelenoede

Description

@Seelenoede

Describe the bug
I found, that I run into an issue when I basically call setReadOnlyFields after I call makeRelationship(). This should normally not a big issue, as you can just change the order of operation, but in our case, we return the record and then mock additional values on the record.

To Reproduce

Basically:

  • We create a basic WebCart record and build the record
  • We process it further by adding CartItems
  • We process it even further by adding CartTaxes
  • between all steps, the record is passed, instead of a builder object

Now in our case, we create CartDeliveryGroups in step 1 and now, I want to set TotalProductCount in step 2, which fails with System.JSONException: QueryResult must start with '{'.

The code pretty much looks like this in compressed and simplified form:

public WebCart mockCart(WebCart cart, CartDeliveryGroup deliveryGroup, Decimal quantity) { WebCart[] carts = (List<WebCart>) fflib_ApexMocksUtils.makeRelationship( List<WebCart>.class, new List<WebCart>{ cart }, CartDeliveryGroup.CartId, new List<List<CartDeliveryGroup>>{ new List<CartDeliveryGroup>{ deliveryGroup } } ); cart = carts[0]; cart = (WebCart) fflib_ApexMocksUtils.setReadOnlyFields(cart, WebCart.class, new Map<String, Object> { 'TotalProductCount' => quantity }); return cart; }

I am pretty sure, you can basically adjust the code for makeRelationship() to also use InjectChildrenEventHandler for setReadOnlyFields(), but I didn't try this out yet.

Expected behavior
setReadonlyFields() should also run on a record with set children.

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