chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I am trying to Mock System.Configuration.ConfigurationSettings so that I can return a configuration section that doesn't exist in the app config file.

This is what I have:

Mock configurationMock = MockManager.Mock(typeof(System.Configuration.ConfigurationSettings));
configurationMock.ExpectAndReturn("GetConfig", filterCriteriaConfiguration);

Where filterCriteriaConfigration is a config object that I have created.

But when my code calls ConfigurationSettings.GetConfig(), nothing gets returned.

Is it a problem mocking sealed classes?
asked by jnapier (9.6k points)

3 Answers

0 votes
Hi,

I am trying to Mock System.Configuration.ConfigurationSettings
...
Is it a problem mocking sealed classes?


There is no problem mocking sealed classes, the problem is that the test framework uses ConfigurationSettings before you set the first mock.

TypeMock doesn't inject code until MockManager.Init is called (This is by design, so that code that isn't part of the test is not injected, and to enable TypeMock to send some vital information to the Injecter)
If you use the NUnit.console the test will actually work (because it doesn't call ConfigurationSettings).

This feature enhancment is already part of our requirements that will be implemented in the future.
Untill we add this feature you can solve this by changing the production code, For example change:
object filter = ConfigurationSettings.GetConfig(); 

to
object filter = GetFilter();
...
private object GetFilter()
{ 
   return ConfigurationSettings.GetConfig(); 
}

and then mock GetFilter in your tests.
answered by richard (3.9k points)
0 votes
Ok. The work around solves the problem and makes the code easier to test.

Thanks.
answered by jnapier (9.6k points)
0 votes
Thanks.

Your welcome :-)
answered by richard (3.9k points)
...