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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:33:02+00:00 2026-06-07T17:33:02+00:00

For example I have SomeClass class which implements ISomeInterface interface with two properties Prop1

  • 0

For example I have SomeClass class which implements ISomeInterface interface with two properties Prop1 and Prop2 (Prop1 has NO setter!!! and Prop2 depends of Prop1):

public class SomeClass : ISomeInterface
    {
        private readonly NameValueCollection settings = ConfigurationManager.AppSettings;
        private readonly string p1;
        private string p2;

        public string Prop1 { get { return settings["SomeSetting"]; } }
        public string Prop2
        {
            get
            {
                switch(Prop1)
                {
                    case "setting1": return "one";
                    case "setting2": return "two";
                    case "setting3": return "three";
                    default: return "one";
                }
            }
            set { p2 = value; }
        }
    }

I need to write unit tests for this class. I suggest they should look like:

[TestMethod]
public void Prop2ShouldReturnOneValueIfSomeSettingEqualsSetting1()
{
    var someClass = new SomeClass();
    Assert.Areequals(someClass.Prop2, "one");
}

[TestMethod]
public void Prop2ShouldReturnTwoValueIfSomeSettingEqualsSetting2()
{
    var someClass = new SomeClass();
    Assert.Areequals(someClass.Prop2, "two");
}

So my question is: How can I force Prop1 return setting1, setting2 etc. if it has no setter? I cannot set the values I need to fields because they are private. Should I mock ISomeInterface or ConfigurationManager??? If so, how can I mock ConfigurationManager if it has no interface??? Should I mock directly ConfigurationManager class??? Will it be right??? Also I don’t understand how can I mock ISomeInterface. I need to test prop2 which depends on prop1. If I type:

[TestMethod]
public void Prop2ShouldReturnTwoValueIfSomeSettingEqualsSetting2()
    {
        var moqSettings = new Mock<ISomeInterface>();
        moqSettings.Setup(p => p.Prop1).Returns("setting2");

        ...
    }

it will not change anything, because moqSettings.Object.Prop2 will be null. And, I’m not sure, but I think it’s wrong to do like that))) So how can I test all cases in prop2???

P.S. For mocking I use Moq.
P.P.S. Sorry for my English))) I hope I explained clearly what i need…

  • 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-06-07T17:33:03+00:00Added an answer on June 7, 2026 at 5:33 pm

    I think your class has several responsibilities – reading configuration, and generating some values depending on configuration settings. If you split those responsibilities, then you can mock your application settings. Create some interface like ISomeConfiguration

    public interface ISomeConfiguration
    {
        string SomeSetting { get; }
    }
    

    Implement it this way:

    public class MyConfiguration : ISomeConfiguration
    {
        readonly NameValueCollection settings = ConfigurationManager.AppSettings;
    
        public string SomeSettings
        {
            get { return settings["SomeSetting"]; }
        }
    }
    

    And inject this interface to your SomeClass:

    public class SomeClass : ISomeInterface
    {
       private readonly string p1;
       private string p2;
       private ISomeConfiguration config;
    
       public SomeClass(ISomeConfiguration config)
       {
           this.config = config;
       }
    
       public string Prop1 { get { return config.SomeSetting; } }
       public string Prop2
       {
           get
           {
              switch(Prop1)
              {
                  case "setting1": return "one";
                  case "setting2": return "two";
                  case "setting3": return "three";
                  default: return "one";
              }
           }
           set { p2 = value; }
      }
    

    }

    Now you can mock your configuration without problems:

    [TestMethod]
    public void Prop2ShouldReturnTwoValueIfSomeSettingEqualsSetting2()
    {
        var moqConfig = new Mock<ISomeConfiguration>();
        moqConfig.Setup(p => p.SomeSetting).Returns("setting2");
        var sut = new SomeClass(moqConfig.Object);
        Assert.That(sut.Prop2, Is.EqualTo("two"));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example, I have a class that has two strings, one of which must
I have class SomeClass with properties. For example id and name : class SomeClass(object):
I have a class which has a DependencyProperty member: public class SomeClass : FrameworkElement
Suppose we have some class A that implements some interface I I i =
For example, I have the following code: SomeClass stub = Mockito.mock(SomeClass.class); After that, stub
I have simple code witch works ok: public class Example { private List<SomeClass> _list
A really simple question. For example, I have a class like this: .someclass {
For example, I have some class hierarchy (possibly, with all kinds of inheritance -
Example: I have an class that inherits from UIImageView. An object creates an instance
Example: I have a selector like this, which I give to another method as

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.