From bb6546875a83d4f589d043cae805dece13d6e151 Mon Sep 17 00:00:00 2001 From: Svetlana Nikulina Date: Tue, 26 May 2026 13:29:49 +0200 Subject: [PATCH 1/5] Add files via upload --- README.md | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..688c8f0 --- /dev/null +++ b/README.md @@ -0,0 +1,54 @@ +# XAF - How to Unit Test Controllers, Actions, and Business Logic (EF Core) + +This example creates unit tests for an XAF application that uses Entity Framework Core as its ORM. The solution illustrates best practices for testing XAF Controllers, Actions, business object behavior, and security-related scenarios using [xUnit](https://www.nuget.org/packages/xunit/) and [Moq](https://www.nuget.org/packages/Moq/). + +## Implementation Details + +The E2E test project covers the following testing patterns: + +| Test Class | What It Tests | +|---|---| +| [`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. | +| [`SetTaskActionExecuteTest`](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/SetTaskActionExecuteTest.cs) | The test runs a `SingleChoiceAction` and updates properties on selected objects. | +| [`SetTaskActionStateTest`](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/SetTaskActionStateTest.cs) | The test evaluates action state based on security permissions through `ISecurityStrategyBase`. | +| [`FindBySubjectActionExecuteTest`](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/FindBySubjectActionExecuteTest.cs) | The test runs a `ParametrizedAction` that finds and displays an object by criteria. | +| [`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. | +| [`CaptionHelperTests`](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/CaptionHelperTests.cs) | The test sets up `CaptionHelper` localization through `ModelApplication` and `ExpressApplicationSetupParameters`. | + +### Testing Actions in Views (Mocking Approach) + +The tests mock `IObjectSpace`, `XafApplication`, `Frame`, and View objects (`DetailView`/`ListView`) to evaluate action behavior without running the full application. Key mocking patterns include: + +- Mocking `IObjectSpace` and its LINQ query methods for object retrieval. +- Creating `DetailView`/`ListView` instances with mocked `CollectionSource` and `ListEditor`. +- Setting up `Frame` objects to activate controllers and register actions. +- Verifying `CommitChanges()` is called after action execution. + +### Testing Security-Dependent Logic + +`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. + +### Testing Event Subscriptions + +`CreateLinkedSaleBaseDescendantControllerTest` uses reflection to verify that a controller correctly subscribes to the `NewObjectViewController.ObjectCreated` event. + +## Files to Review + +- [ActionsInViewTests.cs](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/ActionsInViewTests.cs) +- [SetTaskActionExecuteTest.cs](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/SetTaskActionExecuteTest.cs) +- [SetTaskActionStateTest.cs](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/SetTaskActionStateTest.cs) +- [FindBySubjectActionExecuteTest.cs](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/FindBySubjectActionExecuteTest.cs) +- [CreateLinkedSaleBaseDescendantControllerTest.cs](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/CreateLinkedSaleBaseDescendantControllerTest.cs) +- [CaptionHelperTests.cs](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/CaptionHelperTests.cs) + + + +## Does this example address your development requirements/scenarios? + +[](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=XAF_unit-test-ef-core&utm_term=yes) [](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=XAF_unit-test-ef-core&utm_term=no) + +(you will be redirected to DevExpress.com to submit your response) \ No newline at end of file From ee634050d29f4a539dde189c536d36046d9b88a1 Mon Sep 17 00:00:00 2001 From: Svetlana Nikulina Date: Mon, 1 Jun 2026 15:32:49 +0200 Subject: [PATCH 2/5] Update README.md --- README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 688c8f0..652d644 100644 --- a/README.md +++ b/README.md @@ -41,14 +41,12 @@ The tests mock `IObjectSpace`, `XafApplication`, `Frame`, and View objects (`Det - [CreateLinkedSaleBaseDescendantControllerTest.cs](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/CreateLinkedSaleBaseDescendantControllerTest.cs) - [CaptionHelperTests.cs](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/CaptionHelperTests.cs) - +- [EF Core Unit Tests](https://docs.devexpress.com/eXpressAppFramework/405947/debugging-testing-and-error-handling/ef-core-unit-tests) ## Does this example address your development requirements/scenarios? [](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=XAF_unit-test-ef-core&utm_term=yes) [](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=XAF_unit-test-ef-core&utm_term=no) -(you will be redirected to DevExpress.com to submit your response) \ No newline at end of file +(you will be redirected to DevExpress.com to submit your response) From 06c09284654c8d94df41c28ff40206110155ebd6 Mon Sep 17 00:00:00 2001 From: AndreyKozhevnikov Date: Wed, 3 Jun 2026 13:50:05 +0400 Subject: [PATCH 3/5] Fix event subscription in OnDeactivated method --- .../Controllers/CreateLinkedSaleBaseDescendantController.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CS/XAFUnitTestEFCore/XAFUnitTestEFCore.Module/Controllers/CreateLinkedSaleBaseDescendantController.cs b/CS/XAFUnitTestEFCore/XAFUnitTestEFCore.Module/Controllers/CreateLinkedSaleBaseDescendantController.cs index 8510093..84eb39e 100644 --- a/CS/XAFUnitTestEFCore/XAFUnitTestEFCore.Module/Controllers/CreateLinkedSaleBaseDescendantController.cs +++ b/CS/XAFUnitTestEFCore/XAFUnitTestEFCore.Module/Controllers/CreateLinkedSaleBaseDescendantController.cs @@ -28,6 +28,6 @@ public void CreateLinkedSaleBase(IObjectSpace objectSpace, SaleBase createdObjec } protected override void OnDeactivated() { base.OnDeactivated(); - newObjectController.ObjectCreated += CreateLinkedSaleBaseDescendantController_ObjectCreated; + newObjectController.ObjectCreated -= CreateLinkedSaleBaseDescendantController_ObjectCreated; } -} \ No newline at end of file +} From 2fef3b3741a7a4f4e916e258ad0d9bf1bc4b2db8 Mon Sep 17 00:00:00 2001 From: Svetlana Nikulina Date: Wed, 3 Jun 2026 11:57:39 +0200 Subject: [PATCH 4/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 652d644..596f38e 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ The E2E test project covers the following testing patterns: | [`SetTaskActionExecuteTest`](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/SetTaskActionExecuteTest.cs) | The test runs a `SingleChoiceAction` and updates properties on selected objects. | | [`SetTaskActionStateTest`](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/SetTaskActionStateTest.cs) | The test evaluates action state based on security permissions through `ISecurityStrategyBase`. | | [`FindBySubjectActionExecuteTest`](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/FindBySubjectActionExecuteTest.cs) | The test runs a `ParametrizedAction` that finds and displays an object by criteria. | -| [`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. | +| [`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. | | [`CaptionHelperTests`](CS/XAFUnitTestEFCore/XAFUnitTestEFCore.E2E.Tests/CaptionHelperTests.cs) | The test sets up `CaptionHelper` localization through `ModelApplication` and `ExpressApplicationSetupParameters`. | ### Testing Actions in Views (Mocking Approach) From cd94af8401d383bf9ac92f9c599ae7b083a75185 Mon Sep 17 00:00:00 2001 From: Svetlana Nikulina Date: Thu, 11 Jun 2026 09:51:23 +0200 Subject: [PATCH 5/5] apply review suggestions --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 596f38e..6ccbac5 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ # XAF - How to Unit Test Controllers, Actions, and Business Logic (EF Core) -This example creates unit tests for an XAF application that uses Entity Framework Core as its ORM. The solution illustrates best practices for testing XAF Controllers, Actions, business object behavior, and security-related scenarios using [xUnit](https://www.nuget.org/packages/xunit/) and [Moq](https://www.nuget.org/packages/Moq/). +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/). ## Implementation Details -The E2E test project covers the following testing patterns: +The E2E test project addresses the following testing patterns: | Test Class | What It Tests | |---|---|