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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:20:19+00:00 2026-05-10T16:20:19+00:00

I have an immutable class with some private fields that are set during the

  • 0

I have an immutable class with some private fields that are set during the constructor execution. I want to unit test this constructor but I’m not sure the ‘best practice’ in this case.

Simple Example

This class is defined in Assembly1:

public class Class2Test {     private readonly string _StringProperty;      public Class2Test()     {         _StringProperty = ConfigurationManager.AppSettings['stringProperty'];     } } 

This class is defined in Assembly2:

[TestClass] public class TestClass {     [TestMethod]     public void Class2Test_Default_Constructor()     {         Class2Test x = new Class2Test();         //what do I assert to validate that the field was set properly?     } } 

EDIT 1: I have answered this question with a potential solution but I’m not sure if it’s the ‘right way to go’. So if you think you have a better idea please post it.

This example isn’t really worth testing, but assume the constructor has some more complex logic. Is the best approach to avoid testing the constructor and to just assume it works if all the tests for the methods on the class work?

EDIT 2: Looks like I made the sample a little to simple. I have updated it with a more reasonable situation.

  • 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. 2026-05-10T16:20:19+00:00Added an answer on May 10, 2026 at 4:20 pm

    Nothing, unless you are using that field. You don’t want over-specification via tests. In other words, there is no need to test that the assignment operator works.

    If you are using that field in a method or something, call that method and assert on that.

    Edit:

    assume the constructor has some more complex logic

    You shouldn’t be performing any logic in constructors.

    Edit 2:

    public Class2Test() {      _StringProperty = ConfigurationManager.AppSettings['stringProperty']; } 

    Don’t do that! =) Your simple unit test has now become an integration test because it depends on the successful operation of more than one class. Write a class that handles configuration values. WebConfigSettingsReader could be the name, and it should encapsulate the ConfigurationManager.AppSettings call. Pass an instance of that SettingsReader class into the constructor of Class2Test. Then, in your unit test, you can mock your WebConfigSettingsReader and stub out a response to any calls you might make to it.

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

Sidebar

Ask A Question

Stats

  • Questions 87k
  • Answers 87k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer http://www.dotnetblogengine.net/ May 11, 2026 at 5:31 pm
  • Editorial Team
    Editorial Team added an answer Everyone else is right that you simply didn't have an… May 11, 2026 at 5:31 pm
  • Editorial Team
    Editorial Team added an answer Your hosting server needs to have .Net Framework 3.5 SP1… May 11, 2026 at 5:31 pm

Related Questions

I have over the course of a few projects developed a pattern for creating
Coming from a Java background, I'm used to the common practice of dealing with
Following on from this question , can someone explain the following in Scala: class
I have what amounts to a multi-dimensional array. int[][][] MyValues; What I want is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.