Ticket T863708
Visible to All Users

How to unit test custom logic in XAF/XPO business classes

created 5 years ago (modified 5 years ago)

[DevExpress Support Team: CLONED FROM T741715: 19.1.3 + UnitTests + Performance Optimizations]

Hi Gusha,

one thing which comes in my mind as for interesting cases would be how to correctly test business classes logic with XPO. would be great to see your recommendations. setup xaftypesinfo / objectspaceprovider? (we use inmemory currently), securitysystem and so on…

Comments (2)
DevExpress Support Team 5 years ago

    Hi Martin,

    Would you please elaborate on what logic you need to unit test? Do you mean that you have business logic in your business class properties' getters/setters? Would you please provide me with their sample implementation so that I can provide you with a precise answer?

    M M
    Martin Praxmarer - DevExpress MVP 5 years ago

      Hi Gosha,

      nothing special here about any business logic - it would simply be interessting about best practices about unittesting business classes. as i said above, your recommendations about setup xaftypesinfo system, objectspaceprovider - security system - and so on - like you did with your current series about testing actions / contollers and so on…

      Answers approved by DevExpress Support

      created 5 years ago

      Hi Martin,

      Does YOUR business logic use TypesInfo, ObjectSpaceProvider, etc.? I believe no. In this case, you don't need to care about these things. This is the main idea of the How to write lightweight unit tests for XAF Actions, Controllers and other custom UI logic series - focus on only your business logic. For example, ObjectSpaceProvider is used to create an ObjectSpace instance in an XAF application. In your business logic, you either use an already created ObjectSpace instance or create it using the Application.CreateObjectSpace method. So, either mock an ObjectSpace instance as described in the How to unit test that object property changes via Actions were successfully committed. Or, mock the Application.CreateObjectSpace method as illustrated in the How to unit test object queries by criteria and Detail View creation article.

      Of course, you may call an XAF method that can't be mocked and uses some XAF internals. In this case, it's necessary to research our code to find suitable properties/methods to mock. For example, the approach from the How to unit test localized strings from CaptionHelper (Approach 2) article requires an ObjectSpaceProvider instance. But, it's just mocked.

      As for the XAF Security System, you can also mock it as described in the How to unit test Action's enabled/disabled state based on user permissions article.

      Please let me know if you have further questions.

        Show previous comments (3)
        DevExpress Support Team 5 years ago

          Hi Martin,

          I'd like to inform you that we just published a new article on unit-testing logic in XAF/XPO business classes. Please check How to unit test custom logic in XAF/XPO business classes. We recommend using InMemoryDataStore in such scenarios as you already do.

          We would like to continue our series on unit tests. Unit-testing logic that involves TypesInfo, SecuritySystem, ObjectSpaceProvider looks interesting. Would you please share a couple of your real scenarios to illustrate how your code uses them so that we can evaluate the best ways to test this logic?

          M M
          Martin Praxmarer - DevExpress MVP 5 years ago

            thx you Gosha - i come back to you if i have a bit more time available!

            DevExpress Support Team 5 years ago

              Sure, Martin. I'm looking forward to your response.

              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.