ConfigurationManager.AppSettings not all values

5 posts, 2 answers
  1. Mitch
    Mitch avatar
    6 posts
    Member since:
    Dec 2013

    Posted 14 Aug 2015 Link to this post

    Hi,

     I am using JustMock for some app.config settings:

     Mock.Arrange(() => ConfigurationManager.AppSettings["MyAppSettingKey1"]).Returns("MyAppSettingValue1");​

    Mock.Arrange(() => ConfigurationManager.AppSettings["MyAppSettingKey2"]).Returns("MyAppSettingValue2");​

    Mock.Arrange(() => ConfigurationManager.AppSettings["MyAppSettingKey3"]).Returns("MyAppSettingValue3");​​

     

    However, if I do this, other values that are not explicitly mocked are not available:

    var myValue = ConfigurationManager.AppSettings["MyAppSettingKeyNotMocked"])

    throws a System.NullReferenceException

     

    Is there a better way to mock Configuration.AppSettings?

  2. Answer
    Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 17 Aug 2015 Link to this post

    Hi Mitch,

    You're mocking more than you need. Try this:
    var appSettings = ConfigurationManager.AppSettings;
    Mock.Arrange(() => appSettings["MyAppSettingKey1"]).Returns("MyAppSettingValue1");​
    Mock.Arrange(() => appSettings["MyAppSettingKey2"]).Returns("MyAppSettingValue2");​
    Mock.Arrange(() => appSettings["MyAppSettingKey3"]).Returns("MyAppSettingValue3");​​
    The difference is that I'm now only mocking the AppSettings instance, but not the value of the ConfigurationManager.AppSettings property, which will retain the original behavior for unarranged keys.

    Regards,
    Stefan
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. DevCraft R3 2016 release webinar banner
  4. Mitch
    Mitch avatar
    6 posts
    Member since:
    Dec 2013

    Posted 18 Aug 2015 in reply to Stefan Link to this post

    Hi Stefan,

    thank you for your reply.  This does indeed work and solves my problem perfectly.  I don't fully understand your solution though.
    I fail to see the difference between:

    Mock.Arrange(() => ConfigurationManager.AppSettings["MyAppSettingKey1"]).Returns("MyAppSettingValue1");​

    and 

    var appSettings = ConfigurationManager.AppSettings;
    Mock.Arrange(() => appSettings["MyAppSettingKey1"]).Returns("MyAppSettingValue1");​

     

    Why does this retain the original behaviour for unarranged keys when my initial version doesn't?  I realise you are mocking the instance, but not of my subsequent code refers to this new variable directly.

  5. Answer
    Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 18 Aug 2015 Link to this post

    Hello Mitch,

    This expression:
    Mock.Arrange(() => ConfigurationManager.AppSettings["MyAppSettingKey1"])
    is an example of JustMock's recursive mocking capability. This expression actually arranges two things. First, it arranges the value returned by the ConfigurationManager.AppSettings property to be replaced by a mock collection. Second, it arranges the indexed key on that mock to return some value.

    When you take out the property access outside the arrangement expression:
    var appSettings = ConfigurationManager.AppSettings;
    Mock.Arrange(() => appSettings["MyAppSettingKey1"])
    then you're working with the real AppSettings object, instead of replacing it with a mock collection. Now you're arranging the indexer on the real AppSettings object, instead of on the mock collection.

    Regards,
    Stefan
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  6. Mitch
    Mitch avatar
    6 posts
    Member since:
    Dec 2013

    Posted 19 Aug 2015 in reply to Stefan Link to this post

    Hi Stefan,

     thank you very much for a really clear explanation.  I now completely understand.

     Thanks again.

Back to Top
DevCraft R3 2016 release webinar banner