1

I'm new to Dynamics 365 and started recently to test my code using fake XRM.

I've just run into a problem related to calculations done on currency (Money) fields, for which additional field is being created (which name ends with '_Base'). In unit tests the base of currency field is not being recalculated.

The scenario is: - Code under test sets the "turnover" field of type "Money" on "Account" entity. - Later the "turnover" base currency parts are summed up. (Accounts are refreshed in the context before summing up)

localAccount.turnover = new Money(100);

...

var sumOfTurnovers = localAccounts.Sum(s => s.turnover_Base?.Value ?? 0); // <-- returns 0 in unit tests

Result: the sum of turnovers is calculated as 0, while on real dev environment it is calculated correctly.

It often happens that the code under test relies on recalculated values.

How to emulate re-calculation for such base currency fields in unit tests?

A.D.
  • 295
  • 2
  • 12

1 Answers1

0

This behavior is not emulated by the FakeXrmEasy. Probably because it depends on the environment - on what is the base currency and what is the exchange rate.

Depending on what you are trying to achieve you'll need to calculate the base currency fields up-front during arranging the context. Something like

SetMoneyField(string fieldName, decimal amount, decimal exchangeRate) {
  SetAttributeValue(fieldName, amount);
  SetAttributeValue(fieldName+"_base", amount*exchangeRate);
}
mivra
  • 1,135
  • 10
  • 26