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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:21:13+00:00 2026-05-11T08:21:13+00:00

I am working on a lib that I want to release in open source.

  • 0

I am working on a lib that I want to release in open source. I have started writing the tests for the code, and I was wondering how I am suppose to test a property in a .Net object. Lets say I have the following:

  public class Person{     #region variables     private string _name = String.Empty;     private string _surname = String.Empty;     #region properties     public string Name{         get{              return _name;         }     }     public string Surname{         get{             return _surname;         }         set{             _surname = value;         }     } }  

I have two questions related to the code:

  1. How do I Unit test a Property that just has a getter (Like Name in the example)
  2. How do I Unit test a Property with a setter and a getter (Like Surname in the example)

I want to test properties that are that simple because I have already found errors in other code were Itellinsense did the wrong autocomplete and the property was not returning the correct variable.

Update:

I am not talking about simple properties as the one in the example, they do have some logic behind them and are quite hard to debug. Writing a test that uses the setter to test the getter and vice versa is not good because if there is a fail I won’t know which method to blame. I am using properties because they were added as public variables and later more logic had to be added.

  • 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-11T08:21:14+00:00Added an answer on May 11, 2026 at 8:21 am

    How do I Unit test a Property that just has a getter (Like Name in the example)

    Really not so different from testing if you had a setter. you’ll just need to find another way of determining the output. Could be in a ctor, or the result of other setters/operations on the object.

    [Test] public void NamePropTest() {     Person p = new Person();      //Some code here that will set up the Person object     //  so that you know what the name will be      Assert.AreEqual('some known value...', p.Name); } 

    If we had setters for Name and SurName, but only a getter for FullName, the test could look like this:

    [Test] public void NamePropTest() {     Person p = new Person();      p.Name = 'Sean';     p.Surname = 'Penn';      Assert.AreEqual('Sean Penn', p.FullName); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Most mulitply-defined-symbol error situations tend to be caused by including… May 13, 2026 at 6:59 am
  • Editorial Team
    Editorial Team added an answer The StandardAnalyzer/Tokenizer is the culprit here - it does it's… May 13, 2026 at 6:59 am
  • Editorial Team
    Editorial Team added an answer Random guess: List<SavedOption> finalSavedOptions = savedOptions.Where(x => itemOptions.Any(y => OptionTextDoesMatch(y,… May 13, 2026 at 6:59 am

Related Questions

I am working on a Java project that I want to deliver to my
First a bit of background. I have been working on the MS platform for
This is driving me nuts. I have been at it for over 2 hours
I am calling a csh script that the first thing it does is starts

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.