Last active
December 17, 2015 11:19
-
-
Save gabrieljoelc/5601572 to your computer and use it in GitHub Desktop.
Here's a static method design that allows method mocking/verification via Daniel Cazzulino here http://blogs.clariusconsulting.net/kzu/making-extension-methods-amenable-to-mocking/.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class MessagingExtensions | |
{ | |
public static IMessaging Messaging(this SomeType target) | |
{ | |
return MessagingFactory(target); | |
} | |
static MessagingExtensions() | |
{ | |
MessagingFactory = st => new Messaging(st); | |
} | |
internal static Func<SomeType> MessagingFactory { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestClass] | |
public class StaticMethodTests | |
{ | |
[TestMethod] | |
public void Can_mock_static_method() | |
{ | |
var mockMessaging = new Mock(); | |
MessagingExtensions.MessagingFactory = st => mockMessaging.Object; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment