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.

Add Comment

Required fields are marked *. Your email address will not be published.