|
| 1 | +# XAF - How to Unit Test Controllers, Actions, and Business Logic (EF Core) |
| 2 | + |
| 3 | +This example creates unit tests for an Entity Framework Core ORM-powered XAF application. The solution illustrates best practices for testing XAF Controllers, Actions, business object behavior, and security-related usage scenarios via [xUnit](https://www.nuget.org/packages/xunit/) and [Moq](https://www.nuget.org/packages/Moq/). |
| 4 | + |
| 5 | +## Implementation Details |
| 6 | + |
| 7 | +The E2E test project addresses the following testing patterns: |
| 8 | + |
| 9 | +| Test Class | What It Tests | |
| 10 | +|---|---| |
| 11 | +| [`ActionsInViewTests`](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/ActionsInViewTests.cs) | The test checks action enable and disable behavior based on `TargetObjectsCriteria` and `SelectionDependencyType` in Detail and List Views. | |
| 12 | +| [`SetTaskActionExecuteTest`](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/SetTaskActionExecuteTest.cs) | The test runs a `SingleChoiceAction` and updates properties on selected objects. | |
| 13 | +| [`SetTaskActionStateTest`](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/SetTaskActionStateTest.cs) | The test evaluates action state based on security permissions through `ISecurityStrategyBase`. | |
| 14 | +| [`FindBySubjectActionExecuteTest`](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/FindBySubjectActionExecuteTest.cs) | The test runs a `ParametrizedAction` that finds and displays an object by criteria. | |
| 15 | +| [`CreateLinkedSaleBaseDescendantControllerTest`](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/CreateLinkedSaleBaseDescendantControllerTest.cs) | The test verifies event subscription and copies properties from parent objects to child objects in nested frames. | |
| 16 | +| [`CaptionHelperTests`](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/CaptionHelperTests.cs) | The test sets up `CaptionHelper` localization through `ModelApplication` and `ExpressApplicationSetupParameters`. | |
| 17 | + |
| 18 | +### Testing Actions in Views (Mocking Approach) |
| 19 | + |
| 20 | +The tests mock `IObjectSpace`, `XafApplication`, `Frame`, and View objects (`DetailView`/`ListView`) to evaluate action behavior without running the full application. Key mocking patterns include: |
| 21 | + |
| 22 | +- Mocking `IObjectSpace` and its LINQ query methods for object retrieval. |
| 23 | +- Creating `DetailView`/`ListView` instances with mocked `CollectionSource` and `ListEditor`. |
| 24 | +- Setting up `Frame` objects to activate controllers and register actions. |
| 25 | +- Verifying `CommitChanges()` is called after action execution. |
| 26 | + |
| 27 | +### Testing Security-Dependent Logic |
| 28 | + |
| 29 | +`SetTaskActionStateTest` demonstrates how to mock `ISecurityStrategyBase` to simulate granted or denied field-level permissions and verify that action state reflects the current user's access rights. |
| 30 | + |
| 31 | +### Testing Event Subscriptions |
| 32 | + |
| 33 | +`CreateLinkedSaleBaseDescendantControllerTest` uses reflection to verify that a controller correctly subscribes to the `NewObjectViewController.ObjectCreated` event. |
| 34 | + |
| 35 | +## Files to Review |
| 36 | + |
| 37 | +- [ActionsInViewTests.cs](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/ActionsInViewTests.cs) |
| 38 | +- [SetTaskActionExecuteTest.cs](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/SetTaskActionExecuteTest.cs) |
| 39 | +- [SetTaskActionStateTest.cs](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/SetTaskActionStateTest.cs) |
| 40 | +- [FindBySubjectActionExecuteTest.cs](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/FindBySubjectActionExecuteTest.cs) |
| 41 | +- [CreateLinkedSaleBaseDescendantControllerTest.cs](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/CreateLinkedSaleBaseDescendantControllerTest.cs) |
| 42 | +- [CaptionHelperTests.cs](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/CaptionHelperTests.cs) |
| 43 | + |
| 44 | +## Documentation |
| 45 | + |
| 46 | +- [EF Core Unit Tests](https://docs.devexpress.com/eXpressAppFramework/405947/debugging-testing-and-error-handling/ef-core-unit-tests) |
| 47 | + |
| 48 | +## Does this example address your development requirements/scenarios? |
| 49 | + |
| 50 | +[<img src="https://www.devexpress.com/support/examples/i/yes-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=XAF_unit-test-ef-core&utm_term=yes) [<img src="https://www.devexpress.com/support/examples/i/no-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=XAF_unit-test-ef-core&utm_term=no) |
| 51 | + |
| 52 | +(you will be redirected to DevExpress.com to submit your response) |
0 commit comments