KB Article T848996
Visible to All Users

How to write lightweight unit tests for XAF Actions, Controllers and other custom UI logic

When Quality Matters. Unit & Functional Testing Delivered.
XAF ships with flexible unit & functional testing options for XAF's primary target audience - development teams that build complex enterprise apps.

  • Effective testability with the help of a modular MVC architecture.
  • XAF gives you the freedom to write fast & lightweight unit tests or advanced integration tests as necessary.
  • Ships with a cross-Platform test engine for automated functional tests with C#, VB.NET or a human-readable scripting language.
  • Ready for Continuous Integration systems like Azure DevOps, NUnit, Moq and other popular testing frameworks.
  • Comprehensive documentation, with community and technical support to help you succeed from day one.

Table Of Contents

  1. How to write lightweight unit tests for XAF Actions, Controllers, and other custom UI logic (current)
  2. How to unit test Action's enabled/disabled state based on user permissions
  3. How to unit test whether object property changes via Actions were successfully committed
  4. How to unit test object queries by criteria and Detail View creation
  5. How to unit test event handlers in Controllers
  6. How to unit test New Action's custom business logic based on parent and nested Views
  7. How to unit test Action's enabled/disabled state based on target criteria and selection dependency types in List View
  8. How to unit test Action's enabled/disabled state based on target criteria and selection dependency types in Detail View
  9. How to unit test localized strings from CaptionHelper (Approach 1)
  10. How to unit test localized strings from CaptionHelper (Approach 2)
  11. How to unit test custom logic in XAF/XPO business classes

Fast & Lightweight Unit Tests with Mocks vs Slow & Heavy Unit Tests
The approaches like How to: Test an Action are heavy and complicated to test your custom logic, because they require the following:

  • creating an XafApplication instance;
  • initializing XAF's types info system,
  • creating an object space (usually with an InMemoryDataStore), etc.

To simplify this process, this series demonstrates how to use mocks provided by the Moq library.

Mocking Framework & Used APIs
Moq is a free and popular mocking framework for .NET. Follow their Quickstart article to get an idea of how this library works. You can add it to your test project via NuGet. We will use the following API in this series.

  • new Mock<T> - provides a mock for T.
  • Mock<T>.Setup - sets up method arguments or restricts a method call.
  • Mock<T>.SetupGet - sets up a property.
  • Returns - specifies the method or property result value.
  • Match.Create - creates and sets up conditions for a method parameter.
  • Mock.As - adds an interface implementation to a mock.
  • Verify - checks if a target method or property has been called with specific parameters.
  • Verifiable - marks a method or property as verifiable when the Verify method is called.
  • It - the base class to specify a matching condition for an argument in a method or property.
  • It.IsAny - checks if an argument is any object of the given type.
  • It.Is - checks if an argument meets the given predicate.

Unit Testing Framework & Used APIs
To write tests, we will use the NUnit framework, also available via NuGet. A class with tests is decorated with the TestFixture attribute in NUnit. A test method is decorated with the Test attribute in NUnit. We will also decorate test methods with the TestCase attribute to methods with a set of parameters. Also, we will need run certain methods before and after each tests. Such methods should be decorated with the SetUp and TearDown attributes respectively. To make assertions, we will use the NUnit Assert class.

Learn More About Functional & Unit Testing in XAF
Functional Testing | EasyTest Basics | Script Reference
EasyTest - How to integrate XAF functional testing with Continuous Integration systems like Azure DevOps
How to use EasyTest and Selenium WebDriver to create functional tests for XAF's Blazor UI with C#, VB.NET or a human-readable scripting language
EasyTest - Syntax highlighting, collapsible regions, and code snippets in functional test files (*.etc, *.inc) opened with Notepad++ and Visual Studio Code
T is for Testing: XAF & XPO by Manuel Grundner (DevExpress MVP)

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.