How to unit test SOOrderEntry extension in Acumatica

Hello everybody,

today I want to show to unit test, and I mean really unit test SOOrderEntry graph extnesion in Acumatica with XUnit.

In order to achieve it, you'll need following steps:

  1. Create .Net Class library
  2. Reference xUnit
  3. Create public class that inherits from TestBase class
  4. Add override ResisterServices 
  5. Create something like PrepareGraph with usage of TestBase.Setup class
  6. Write your mehtods.

I will not describe how steps 1 - 4 may look, as it is pretty obvious, but step 5 and 6 at C# level may look like this:

public class SOOrderEntrySDExtTests : TestBase
{
    protected IPXCurrencyService CurrencyService;
    protected IFinPeriodRepository FinPeriodService;
 
    public SOOrderEntryExtTests()
    {
        FinPeriodService = new PX.Objects.Unit.FinPeriodServiceMock();
        CurrencyService = new PX.Objects.Unit.CurrencyServiceMock();
    }
 
    protected override void RegisterServices(ContainerBuilder builder)
    {
        base.RegisterServices(builder);
        builder
            .Register<Func<PXGraphIFinPeriodRepository>>(context
                =>
            {
                return (graph)
                    =>
                {
                    return FinPeriodService;
                };
            });
        builder
            .Register<Func<PXGraphIPXCurrencyService>>(context
                =>
            {
                return (graph)
                    =>
                {
                    return CurrencyService;
                };
            });
    }
 
    private SOOrderEntry PrepareGraph()
    {
        Setup<SOOrderEntry>(
           new SOSetup
           {
               DefaultOrderType = "SC",
               TransferOrderType = "TR",
               ShipmentNumberingID = "SOSHIPMENT",
               ProrateDiscounts = true,
               FreeItemShipping = "S",
               FreightAllocation = "A",
               CreditCheckError = false,
               MinGrossProfitValidation = "W"
           });
        
        
        var graph = PXGraph.CreateInstance<SOOrderEntry>();
        graph.CurrentDocument.Insert(
            new SOOrder()
            {
                OrderType = "SC",
                OrderDesc = "some test desc"
            }
            );
        var graphExtension = graph.GetExtension<SOOrderEntryExt>();
        
 
        return graph;
    }
 
    [Fact]
    public void CheckInsertion()
    {
        var graph = PrepareGraph();
        graph.CurrentDocument.Current.Approved = true;
        graph.CurrentDocument.Update(graph.CurrentDocument.Current);
 
        Assert.Equal(graph.CurrentDocument.Current.CuryID, "USD");
 
    }
}

Few more comments regarding code. 

  1. As of now, in order to give to SOOrderEntry setup classes, you'll need to go from one exception message to another. Not very convenient, but the only available way
  2. RegisterServices was copy/pasted from Github post of Dmitriy Naumov
  3. You may need to take a look on graphExtnesion
  4. Regarding extension fields I suggest to set up them in the cache

Summary

Acumatica team put big amount of efforts in order to add stability to their products. Also they put a lot of efforts for giving line for adding stability to Acumatica by other ISV. It will be a crime not to benefit from it. Crime against customers!

No Comments

Add a Comment