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 have a singleton that I am using in some of my tests. This singleton is exposed as a property of a static class. Several of my tests attmept to mock calls on the singleton like this:

recorder.ExpectAndReturn(ApplicationScope.Settings.MailFromServerAddress, fromEmailAddress);


Where the Settings property on ApplicationScope is the reference to the singleton.

All of my tests run fine except for the first one that tries to mock the call to the Settings singleton. This is the exception I get from my test

TypeMock.VerifyException:
TypeMock Verification: Method GameFly.Retail.Commons.Configuration.ApplicationSettings.get_MailFromServerAddress() has 1 more expected calls.


I have run a trace and see that the call is mocked, the problem is that the actuall call is not trapped by the TypeMock engine. I saved the trace out to a file if you would like to see it. All the subsequent tests that perform similiar operations work as expected. But if I run one of these tests by themselves, the test will fail as well. I am assumming this has something to do with the singleton nature of the settings object and something that I am failing to mock. Thanks for any help.
asked by juice_johnson (9.3k points)

3 Answers

0 votes
Please post the Settings code and the singleton decliration and initialization code.
answered by scott (32k points)
0 votes
The code of the class is to much to post but here are the important parts

Singleton initialization:
private static readonly ApplicationSettings _instance = Singleton<ApplicationSettings>.Instance;


Singleton access:
public static ApplicationSettings Instance{
         get {
            return ApplicationSettings._instance;
         }
      }


Sample Setting property:


public string SmtpHostAddress {
         get {
            if (this._smtpHostAddress == null) {
               this._smtpHostAddress = this.SafeString("SmtpHostAddress", "127.0.0.1");
            }
            return this._smtpHostAddress;
         }
      }



Also here is the code for the genric singleton class whcih Im sure you will want to see:

public static class Singleton<T> where T : class{

      public static readonly T Instance = typeof(T).InvokeMember(typeof(T).Name,
                     BindingFlags.CreateInstance |
                     BindingFlags.Instance |
                     BindingFlags.NonPublic, null, null, null, CultureInfo.InvariantCulture) as T;
      
   }   


Please let me know how I can be of further help in resolving this. Thank you.
answered by juice_johnson (9.3k points)
0 votes
Taken offline for patch.
answered by scott (32k points)
...