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;
}