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

  • Home
  • SEARCH
  • 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 789867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:35:32+00:00 2026-05-14T21:35:32+00:00

I would like to test the validation of submitted DTO. This is the bare

  • 0

I would like to test the validation of submitted DTO. This is the bare bone of a controller create action:

[AcceptVerbs(HttpVerbs.Post)]
        public RedirectToRouteResult Create(SomeDTO SomeDTO)
        {
            SomeObject SomeObject = null;

            try
            {
SomeObject = this.RepositoryService.getSomeObjectRepository().Create(SomeDTO, this.RepositoryService);
            }
            catch (BrokenRulesException ex)
            {
                ex.AddModelStateErrors(ModelState, "Model");
            }
            catch (Exception e)
            {
                ModelState.AddModelError("Exception", e.Message);
            }

            TempData["ViewData"] = ViewData;
            TempData["SomeDTO "] = SomeDTO;

            return ModelState.IsValid ? RedirectToAction("SomeObjectDetail", new { Id = SomeObject.Id }) : RedirectToAction("Form");
        }

The mechanics , although not relevant, is as follows: I have a strongly typed view = form which submits a dto to this action which either returns the form or the details page of the created object.

I would like to unit test whether the Model contains certain key/errorMessage combinations given some invalid dto. Did someone do similar stuff? Any pointers would be very much appreciated.

Thanks.

Best wishes,

Christian

  • 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-14T21:35:33+00:00Added an answer on May 14, 2026 at 9:35 pm

    In your case, I don’t think you really want to test the model state. Rather, you should simply use the test set up to induce a correct/incorrect model state, then test that the appropriate result is returned under the conditions. You can use separate tests to validate the various conditions under which the model is valid/invalid, though, if you use DataAnnotations, I don’t see much point in testing that they and the model binding infrastructure work correctly. Presumably Microsoft has already done this.

    Original, based on the (faulty) assumption that a ViewResult was being returned

    I think it should be something like this.

    [TestMethod]
    public void TestCreate()
    {
         // set up
    
        var result = Create( invalidDTO ) as ViewResult;
    
        var modelState = result.ViewData.ModelState;
    
        Assert.IsFalse( modelState.IsValid );
    
        var errors = modelState.Errors;
    
        Assert.AreEqual( 1, errors.Count );
        Assert.AreEqual( errors[0].ErrorMessage, "some error message" );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my test for validation, i would like to find the best way
I am using the RegularExpressionAttribute from DataAnnotations for validation and would like to test
I would like to test a couple of regular expressions for email address validation,
i would like to test behat, mink and sahi with symfony2 so, in my
I would like to test if a point is within a particular distance of
I would like to test some exception handling logic in the empty catch block
I would like to test an app that uses the Clipboard (WindowsForms) and I
I would like to test a web app that was drastically redesigned to use
I would like to do a test like: http://stevelibonati.wordpress.com/2012/02/05/spring-mvc-test-with-a-sprinkle-of-mockito/ My problem is that I
I would like to test if an instance variable lies in a range of

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.