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:
- Create .Net Class library
- Reference xUnit
- Create public class that inherits from TestBase class
- Add override ResisterServices
- Create something like PrepareGraph with usage of TestBase.Setup class
- 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<PXGraph, IFinPeriodRepository>>(context
=>
{
return (graph)
=>
{
return FinPeriodService;
};
});
builder
.Register<Func<PXGraph, IPXCurrencyService>>(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.
- 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
- RegisterServices was copy/pasted from Github post of Dmitriy Naumov
- You may need to take a look on graphExtnesion
- 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!