Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4102756
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:52:12+00:00 2026-05-20T20:52:12+00:00

I run all my actions through classes that I call commands. To illustrate this,

  • 0

I run all my actions through classes that I call “commands”. To illustrate this, to create a new user I would call the following code

new CreateUserCommand(unitOfWork).SetName("Username").SetPassword("Blah").Execute();

I am now currently looking at implementing validation into this system, to validate things such as the password is a certain length, the username is not a duplicate in the database, etc…

To handle this, I am looking at using fluent validations and creating a validation class for each type of entity validation entity I want. For example, I would have a class such as

public class NewUserValidation : ValidationFor<User>
{
   public NewUserValidation() 
   {
     // Validation Rules here
   }
}

One advantage of creating validation in it’s own class is that I can use the same validation rules for multiple commands (for example, editing and creating companies may use the same validation rules).

Now each of these validation classes would have unit tests associated with them. However, I am trying to figure out how to deal with the unit tests for the command classes that utilize these validation classes.

For example, when creating a test for the command class, would I create individual tests for each validation rule for the class (thus essentially duplicating all the unit tests for the validation class itself for every command class I plan to use the same validation class for)? This makes a lot of overhead, especially when I already know that the validation class works because of separate unit tests for them.

The only other option that I see is to expose a public property in the command that holds the validation class, and I would then unit test that the command’s validation class is the expected validation class. The problem with this method though is I need to devise some way to verify that my Execute() method is actually running the validation class (otherwise there is no way to know if validation isn’t being run).

I am still leaning towards the latter testing method, just to help keep the overhead down, but I do need to find a solution for checking if the validation is actually run. Would this be a bad way to go about it, and would I be better going for the former style instead?


Edit:
To reply to both answers below, the validation is going to be used inside of the Execute() method, by a validator.Validate(entity) call in the implementation of Execute().

While I don’t want to violate DRY, I don’t see an easy way to verify that Execute() 1) uses the correct validation class by default and 2) Actually calls the .validate(entity) method of the validation class.

I can solve #1 by instantiating the validation class in the constructor, and expose it through a public property of the command class, but I am unsure of how to correctly unit test for the 2nd issue without repeating the individual validation unit tests.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:52:12+00:00Added an answer on May 20, 2026 at 8:52 pm

    You already test the functionality of the validator classes. So the only thing to test on these command classes is that they actually employ the validator.

    Since how you plan to make use of these validators is not obvious, it is hard to say how this can be done. Replicating the test cases for each command is the least efficient way to go about it, but in the end, it may prove to be the only way to test the presence of validators.


    EDIT: From your recent addition, this looks like a perfect candidate for using mock objects.

    To test the Execute method, you can mock your validators, and verify that their validate methods are called. To test the command construction, you can mock your command object, and check that the appropriate validators are passed to it.

    You may want to check out this question (stackoverflow.com). You may need to massage your design so that you can use a mock framework, though.


    EDIT:
    Also have a look at this question (stackoverflow.com). It seems to address your requirements. The Microsoft tool Moles (microsoft.com) looks very interesting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want this for some conditional compilation code that will run in all IE's
I run all my integers through a (int)Integer to make them safe to use
Application has an auxiliary thread. This thread is not meant to run all the
I know that you can run almost all Java in Dalvik's VM that you
Is it true that in the flash/air player the actionscripts are all run in
I went through this thread but couldn't understand much. I am very new to
I am trying to create an app that will perform actions on specific times
I'm following through the Learn Rails by Example book, and I'm trying to run
Greetings all. I have 2 actions: Find and Run. They can be invoked in
From Eclipse I can easily run all the JUnit tests in my application. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.