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'm trying to mock System.Configuration.ConfigurationManger so that I can mock the call to GetSection but it does not appear that I can mock this call. I keep getting back null even though I have mocked the call.

Here is my code:

Mock mockConfigurationManger = MockManager.MockAll(typeof(ConfigurationManager),Constructor.NotMocked);

//have the configuration manager return our mock configuration
mockConfigurationManager.ExpectAndReturn("GetSection", this.ProviderConfigurationMock.Object).args("connections");


ConfigurationManager is showing up as being mocked in the tracer. The GetSection method is also showing up as expecting one call. I have also verified that the argument is correct.

Any ideas
asked by jnapier (9.6k points)

2 Answers

0 votes
I've done a little more testing on this and I can get it to pass, but only on every second test. I can run the test one time and then it will fail. If I run the test again, it will succeeed. If I run it agian it will fail, and then it will succeedd again. this pattern seems like it could go forever.

Very very weird.
answered by jnapier (9.6k points)
0 votes
Hi,
You have managed to find a case that TypeMock has some difficulties mocking.
What happens is that ConfigurationManager is called whenever an executable is first run (This is before TypeMock is run) and once JITTed there is not much TypeMock can do.
The second time it is run TypeMock is already running and can mock that class.

I would really advise in this case, to use more conventional methods:
For example you can switch your configuration file to the one tested as follows:
Put the test config file in C: estConfigurationManager.exe.config
// Get the machine.config file.
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
// You may want to map to your own exe.comfig file here.
fileMap.ExeConfigFilename = @"C:	estConfigurationManager.exe.config";
System.Configuration.Configuration config =       ConfigurationManager.OpenMappedExeConfiguration(fileMap, 
        ConfigurationUserLevel.None);


Note: This is not the only way to test this, there are other ways to modify the configuration without mocking.
answered by scott (32k points)
...