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

The Archive Base Latest Questions

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

I have a read-only property on a class that I am testing. public string

  • 0

I have a read-only property on a class that I am testing.

    public string ReadOnlyProperty
    {
        get { return _readOnlyProperty; }
    }

Is there a way to write an NUnit test that ensures that this property is readonly? Does the fact that I want to do this at all cause you to raise your eyebrow? It seems to me that adding tests to ensure that read-only properties remain read-only unless a deliberate decision is made to change them is just as important as any other behavior.

Thanks in advance for the feedback.

  • 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-26T21:05:36+00:00Added an answer on May 26, 2026 at 9:05 pm

    It seems to me that adding tests to ensure that read-only properties remain read-only unless a deliberate decision is made to change them is just as important as any other behavior.

    I agree, but I dare say that unit testing is the wrong way. Here’s why:

    Unit testing is generally used to test the dynamic aspects of code, i.e. its run-time behaviour. You, on the other hand, are looking for a way to test a static (compile-time or design-time) aspect of your code. It would seem to me that tools such as FxCop or NDepend are more appropriate in this case. (I may be wrong about these particular tools being appropriate since I don’t know them very well myself.)

    That being said, as you’ve already learned from previous answers, you could do this using reflection:

    typeof(SomeType).GetProperty("ReadOnlyProperty").CanWrite == false
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that defines a read-only property that effectively exposes a private
Let's say I have a Concrete class with a Read-Only property e.g. public class
I've got a class that has a read-only property defined that is actually a
I have a complex value object class that has 1) a number or read-only
In a Python program I have a read-only property that I create using bla
Now that I have a read-only application working, I am working on the insert
I have a read only list that is shared across all instances of the
I want to set a read-only attribute inside a class method. I have already
I have a class with a private set property that I want to stub
I have some read-only data that I want to initialise and then re-initialise periodically

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.