Understanding FakeXrmEasy Test Flow in Dynamics 365 CRM
Why We Add Entities to MergeSalesOrdersBase
When writing unit tests for Dynamics 365 CRM plugins using FakeXrmEasy, one of the most confusing concepts is:
Why do we need to add entities into
_entitieswhen we already use.ToEntityReference()?
This article explains the complete testing flow step by step.
The Real Problem
Suppose you have a Sales Order:
At first glance, it feels like the Sales Order Type already exists.
But internally, that is NOT true.
.ToEntityReference() only stores:
- Logical Name
- GUID
Example:
It does NOT store the actual entity fields.
So fields like:
are NOT available unless the entity itself is added into FakeXrmEasy.
Understanding the Complete Test Flow
Step 1 — Base Class Runs First
Your test inherits from:
So before the actual test executes, the base setup runs:
This method prepares all common entities.
Example:
Think of this as seeding a fake CRM database.
Step 2 — Test Creates a Sales Order
Inside the test:
This only stores a lookup reference.
Equivalent to SQL:
But the actual SalesOrderType row still does not exist.
Step 3 — FakeXrmEasy Initializes Database
When this line runs:
FakeXrmEasy creates an in-memory CRM database using ONLY entities present in _entities.
Current database may contain:
If AcknowledgementTrue was never added, it does not exist in the fake database.
Step 4 — Plugin Executes
Now plugin execution starts:
Inside the plugin, code usually retrieves related entities.
Example:
FakeXrmEasy now searches its in-memory database.
It tries to find:
If entity does not exist:
- Retrieve fails
- Plugin throws exception
- FakeXrmEasy wraps it
- You see errors like:
Why Standard Worked
Because your base setup already contains:
So FakeXrmEasy can successfully retrieve it.
Why AcknowledgementTrue Failed
Because you changed:
but NEVER added:
Therefore the fake CRM database does not contain that row.
Correct Fix
Add the entity before initialization:
Now FakeXrmEasy database contains:
Plugin retrieval succeeds.
Why Put It in MergeSalesOrdersBase
Because MergeSalesOrdersBase acts as:
Shared Test Database Setup
All tests inheriting from it automatically get:
- common master data
- lookup records
- reference entities
- configuration entities
- products
- companies
- sales order types
without repeating setup in every test.
This keeps tests:
- cleaner
- reusable
- maintainable
- easier to debug
Simple Mental Model
| CRM Concept | FakeXrmEasy Equivalent |
|---|---|
| Dataverse Row | _entities.Add() |
| Lookup Field | .ToEntityReference() |
| CRM Database | _context.Initialize(_entities) |
| Plugin Retrieve | service.Retrieve() |
Important Takeaway
EntityReference is NOT the entity itself.
This:
means only:
To make plugin retrieval work, the actual entity must exist in FakeXrmEasy memory:
Recommended Best Practice
Whenever your plugin retrieves related entities:
Always ensure those related entities are added into _entities before:
Especially for:
- lookup entities
- configuration records
- reference/master data
- related parent entities
- option/configuration entities
Final Example
Incorrect
without:
Correct
Now the plugin can retrieve the entity successfully.
Conclusion
Understanding the difference between:
EntityReference- actual entity records
- FakeXrmEasy in-memory database
is one of the most important concepts in Dynamics CRM plugin unit testing.
Comments
Post a Comment