0

I have this test where I need to set a boolean (Requires_number__c) on a WebCart. This boolean is normally set via a trigger, in case a specific cartItem is added to the cart.

The problem I am currently is facing is that the test keeps returning false, eventhough the value has been updated - the value is updated in the CartItemAddHandler, but when I assert the value in the test code it still says it is false, when it is supposed to be true?

trigger code:

trigger CartItemTrigger on CartItem(before insert, after delete) {
  // Add a static variable to control trigger execution during tests
  system.debug('trigger');
  system.debug(CartItemAddHandler.skipTrigger);
  if (!CartItemAddHandler.skipTrigger) {
    Id userId = Id.valueof(UserInfo.getUserId());
    system.debug('inside trigger');

    WebCart cart = [
      SELECT Id, Requires_number__c
      FROM WebCart
      WHERE OwnerId = :userId AND (Status = 'Checkout' OR Status = 'Active')
      WITH SECURITY_ENFORCED
      LIMIT 1
    ];

    CartItemAddHandler cartItemAddHandler = new CartItemAddHandler();

    if (Trigger.isInsert) {
      for (CartItem cartItem : Trigger.new) {
        cartItemAddHandler.NewCartItemAddedToCart(cartItem);
        system.debug('trigger');
      }
    }

    if (Trigger.isDelete) {
      cartItemAddHandler.CartItemWasRemovedFromCart();
    }
  }
  system.debug('trigger end');
}

CartItemAddHandler:

public class CartItemAddHandler {
  public static Boolean skipTrigger = true;

  public void NewCartItemAddedToCart(CartItem cartItem) {
    system.debug('cartItemAdded');
    Id userId = Id.valueof(UserInfo.getUserId());
    WebCart cart = [
      SELECT Id, Requires_number__c
      FROM WebCart
      WHERE OwnerId = :userId AND (Status = 'Checkout' OR Status = 'Active')
      WITH SECURITY_ENFORCED
      LIMIT 1
    ];
    Product2 prod = [SELECT Id, Requires_number__c FROM Product2 WHERE Id = :cartItem.Product2Id LIMIT 1];
    cartItem.Requires_number__c = prod.Requires_number__c;
    if (cartItem.Requires_number__c == true) {
      cart.Requires_number__c = true;
      system.debug('cartItemAdded update');
      update cart;
      system.debug('cartItemAdded updateas');
      system.debug(Cart.Requires_number__c);
    }
  }

  public void CartItemWasRemovedFromCart() {
    Id userId = Id.valueof(UserInfo.getUserId());
    WebCart cart = [
      SELECT Id, Requires_number__c
      FROM WebCart
      WHERE OwnerId = :userId AND (Status = 'Checkout' OR Status = 'Active')
      WITH SECURITY_ENFORCED
      LIMIT 1
    ];
    if (cart.Requires_number__c == true) {
      Boolean hasSoftware = false;
      CartItem[] cartItemsList = [SELECT Id, Requires_number__c FROM CartItem WHERE cartId = :cart.Id AND Requires_number__c = TRUE];
      if (cartItemsList.size() > 0) {
        hasSoftware = true;
      }

      if (hasSoftware == false) {
        cart.Requires_number__c = false;
        update cart;
      }
    }
  }
}

TestCode:

@isTest
public class CartItemAddHandlerTest {
@isTest
  static void TestSoftwareProductAdddedToCartSetRequiresSerialNumberTrueOnCartViaTrigger() {
    system.debug('being tested');
    CartItemAddHandler.skipTrigger = false;
    system.debug('skip to false');
    List<Account> testAccounts = IMPACT_TestDataFactory.createAccount(true, 1);
    system.debug('account');
    WebStore testWebStore = IMPACT_TestDataFactory.createWebstore('Store ', true);
    system.debug('webstore');
    Webcart cart = IMPACT_TestDataFactory.createWebCart(testWebStore.Id, testAccounts[0].Id, true);
    system.debug('cart');
    CartDeliveryGroup cartDeliveryGroup = IMPACT_TestDataFactory.createCartDeliveryGroup(cart.Id, true);
    system.debug('cartdeliverygroup');
    List<Product2> softwareProducts = IMPACT_TestDataFactory.createSoftwareProducts(1, true);
    system.debug('product');
    System.Test.startTest();
    List<CartItem> cartItems = IMPACT_TestDataFactory.createCartItemsBasedOnProducts(cart.Id, cartDeliveryGroup.Id, softwareProducts, false);
    system.debug('cartItem');
    system.debug(cart.Requires_number__c);
    System.assert(cart.Requires_number__c, 'Requires_number__c should be true on the cart');
    System.Test.stopTest();
  }
}

Here is the testData factories I use

  public static List<Product2> createSoftwareProducts(Integer numOfRecords, Boolean requiresNumber) {
    List<Product2> listOfProducts = new List<Product2>();

    for (Integer i = 0; i < numOfRecords; i++) {
      Product2 product = new Product2(
        Name = 'TestProduct' + i,
        Requires_number__c = requiresNumber,
        StockKeepingUnit = generateRandomString(5),
        IsActive = true
      );
      listOfProducts.add(product);
    }

    insert listOfProducts;
    return listOfProducts;
  }

And here is the cartItem I create

 public static List<CartItem> createCartItemsBasedOnProducts(Id cartId, Id cartDeliveryGroupId, List<Product2> products, Boolean skipTrigger) {
    List<CartItem> listOfCartItem = new List<CartItem>();
    CartItemAddHandler.skipTrigger = skipTrigger;
    for (Product2 product : products) {
      CartItem cartItem = new CartItem(
        CartId = cartId,
        Type = 'Product',
        Name = product.Name,
        CartDeliveryGroupId = cartDeliveryGroupId,
        Product2Id = product.Id,
        Requires_robot_serial_number__c = product.Requires_number__c
      );

      listOfCartItem.add(cartItem);
    }

    insert listOfCartItem;

    CartItemAddHandler.skipTrigger = !skipTrigger;

    return listOfCartItem;
  }
3
  • where does your testmethod mock Product2 records?
    – cropredy
    Commented Dec 19, 2023 at 5:34
  • @cropredy added the things that you requested Commented Dec 19, 2023 at 8:19
  • CreateSoftwareProducts are the one that creates the products that has the requires number boolean on. Commented Dec 19, 2023 at 8:23

1 Answer 1

0

I think the problem lies in the below section of code -

if (cart.Requires_number__c == true) {
      Boolean hasSoftware = false;
      CartItem[] cartItemsList = [SELECT Id, Requires_number__c FROM CartItem WHERE cartId = :cart.Id AND Requires_number__c = TRUE];
      if (cartItemsList.size() > 0) {
        hasSoftware = true;
      }

      if (hasSoftware == false) {
        cart.Requires_number__c = false;
        update cart;
      }
    }

hasSoftware is not set to false as there are no records of CartItem which has Requires_number__c= True. Hence you need to create a record of CartItem which has Requires_number__c=True. That will satisfy the below code block -

if (hasSoftware == false) {
   cart.Requires_number__c = false;
   update cart;
}

Set Requires_number__c=True in createCartItemsBasedOnProducts for a single record.

2
  • thats the point? it has to be false if there is no cartitem that requires_number = false. Or what solution are you proposing? Commented Dec 20, 2023 at 8:51
  • that's the point if there is no cart item the hasSoftware always satisfies the if(hasSoftware == false) which will set the requires_number = false. I suggest creating two test methods one in which there is no cart item and in other create cart items with requires_number = true. Commented Dec 21, 2023 at 6:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .