This is a migrated thread and some comments may be shown as answers.

ConfigurationManager.AppSettings not all values

4 Answers 269 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Mitch
Top achievements
Rank 1
Mitch asked on 14 Aug 2015, 12:48 PM

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?

4 Answers, 1 is accepted

Sort by
0
Accepted
Stefan
Telerik team
answered on 17 Aug 2015, 07:31 AM
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
0
Mitch
Top achievements
Rank 1
answered on 18 Aug 2015, 01:29 PM

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.

0
Accepted
Stefan
Telerik team
answered on 18 Aug 2015, 01:43 PM
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
0
Mitch
Top achievements
Rank 1
answered on 19 Aug 2015, 11:16 AM

Hi Stefan,

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

 Thanks again.

Tags
General Discussions
Asked by
Mitch
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Mitch
Top achievements
Rank 1
Share this question
or