Evil Singleton Mocking with Natural™ Mocks

Ok, so we all know that Singletons are Evil, and Hard to test but sometimes you just have to be bad.

We have added some more support for Singletons in the coming TypeMock version.
With Naturalâ„¢ Mocks, it is quite easy to mock a Singleton. Here is how: 

public class MyServer { public string Host { get { return Singleton.Instance.GetHost(); } } } [Test] public void TestSingleton() { string expectedHost = "wow"; using (RecordExpectations record = new RecordExpectations()) { Singleton.Instance.GetHost(); record.Return(expectedHost); } MyServer server = new MyServer(); Assert.AreEqual(expectedHost, server.Host); MockManager.Verify(); }

Couldn’t be simpler:
TypeMock will know that Singleton is a singleton, and will do all the mocking for you.

19 February 2007 | .NET Tests | Comments | Print This Post

Leave a Reply

  1.  
  2.  
  3.  
Search Development and Integrity Management by Eli Lopian

Navigation

Recent Posts

Categories

Archives

Managment