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 4266464
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:45:50+00:00 2026-05-21T06:45:50+00:00

I am having a class which uses the XmlReader and XmlReaderSettings classes in C#

  • 0

I am having a class which uses the XmlReader and XmlReaderSettings classes in C# to validate Xml files against a schema. Since my application involves reading the Xml data from the database, I decide to show an error to the user in a MessageBox. Thus, any validating errors as well as any exceptions thrown would be shown with a string “Error occurred while parsing” appearing in a MessageBox.

I also have a boolean variable which returns whether the parsing was successful or not.

Right now, I am using the boolean value returned by the Parse function in an Assert while having the parse function parse both valid and invalid Xml files.

Thus, while running the suite of test cases, I have these stack of message boxes piled up in the other window.

The real question I had that was it ok to have a number of these message boxes pop up while the unit testing framework in Visual Studio tells us whether all the tests passed or not.

Or is it a case where I just need to return a bool value and then the GUI class displays the appropriate error message.

Q2. Also, say If i do need to check whether a particular string was parsed correctly and stored into an array, can I subclass the main class to add some functionality which could help me in better write Unit tests?

I would really appreciate some suggestions as to how my design and unit testing should be.

Also, I do accept that I am making a big mistake in writing the Unit tests after I have written down the class which I need to test and I know it should be the other way round.

  • 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-21T06:45:51+00:00Added an answer on May 21, 2026 at 6:45 am

    The parsing of the Xml and the displaying of error messages are separate concerns, so your parser should not have any knowledge about how the error messages are displayed.

    Depending on your needs, there are a few options:

    Exceptions

    I often live by the rule: “If a method can’t do it’s job, throw an exception”. If you need to stop at the first error, exceptions are the way to go.

    From a unit test perspective, if you pass in illegal data, verify that the code throws an exception using the [ExpectedException] attribute.

    [TestMethod, ExpectedException(typeof(ParserValidationException))]
    public void IllegalDataShouldThrowValidationErrors()
    {
        var parser = new MyParser();
        parser.Parse( dataThatContainsErrors );
    }
    

    However, if your need to ignore illegal data and report errors, you might want a different approach.

    Specialized Return Type

    If you need to collect all the errors, it’s best to keep the parsed result and the errors together as an object.

    public class ParsedResult<T>
    {
        public T Result;
        public List<string> Warnings;
    }
    

    From a unit test perspective, you should verify that the list of warnings isn’t empty if you pass in illegal data.

    [TestMethod]
    public void ParsedResultsForIllegalDataShouldContainWarnings()
    {
        var parsedResult = new MyParser.Parse<Foo>( dataThatContainsErrors );
    
        Assert.IsNotNull(parsedResult);
        Assert.IsNotNull(parsedResult.Result);
        Assert.AreEqual(1, parsedResult.Warnings.Count);
    }
    

    Error Reporter

    Pass in a collaborator into the object and have it report it’s findings.

    public ObjectToReturn Parse(string xml, IProgressReporter progress)
    {
         // create xml reader
         // read values from xml
         // if a value is invalid, log it
         progress.AddMessage( "property x was invalid. ")
    }
    

    The progress reporter can be a wrapper around your MessageBox, or it could be a Console output, Logger, etc. From a unit test perspective, you can either create a Test Double that captures the messages, or you can use a mock framework and verify that it was called a certain amount of times. Here’s an example that uses Moq.

    var mockReporter = new Mock<IProgressReporter>();
    IProgressReporter reporter = mockReporter.Object;
    
    var parser = new MyParser();
    var illegalData = // your illegal data;
    
    var result = parser.Parse( illegalData, parser);
    
    Assert.IsNotNull(result, "The value was not parsed correctly.");
    mockReporter.Verify( r => r.AddMessage( It.IsAny<string>() ), Times.AtLeast(1));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble deploying .NET application which uses Microsoft Access automation. I've installed the
I have developed a Java Swing application, which uses the SwingWorker class to perform
What is the point of having a dynamic class on which you can call
Does having several levels of base classes slow down a class? A derives B
I'm working on a problem which uses a python class and has a constructor
I have a model which uses acts-as-tree. For example: class CartoonCharacter < ActiveRecord::Base acts_as_tree
I've created a custom VS template which uses an IWizard class to do some
I am developing an application for WebSphere 6.1 which uses a Java servlet. Within
I'm writing a program that uses an Event class, which has in it an
I wanna place two child un-ordered lists side by side. They are having Class

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.