3 ms·
Unfortunately in C# you can only Mock Abstract/Virtual methods and Interfaces. It's pretty annoying TBQH.
by rjbwork 9y ago
Unfortunately in C# you can only Mock Abstract/Virtual methods and Interfaces.
It's pretty annoying TBQH.
- tjalfi 9y agoTypeMock Isolator[0] and JustMock[1] can mock nonvirtual methods and static methods. Fody Ionad[2] is a free tool that can replace static methods. The reason that most mocking framework don't support this is that the implementation is complex[3]. [0] https://www.typemock.com/isolator https://www.typemock.com/isolator [1] http://www.telerik.com/products/mocking.aspx http://www.telerik.com/products/mocking.aspx [2] https://github.com/Fody/Ionad https://github.com/Fody/Ionad [3] http://mattwarren.org/2014/08/14/how-to-mock-sealed-classes-and-static-methods/ http://mattwarren.org/2014/08/14/how-to-mock-sealed-classes-...
- flukus 9y agoI can't see anything changing until the OSS frameworks have similar support. For that matter, MS should take the lead for something that could provide across the board performance improvements on their ecosystem. They don't seem to care about tooling to improve their own platforms.
- JackMorgan 9y agoSimpleMock is a pattern for unit test mocking that uses no interfaces or libraries! It's simple and works very well. http://deliberate-software.com/simplemock-unit-test-mocking/ http://deliberate-software.com/simplemock-unit-test-mocking/
- bonesss 9y agoI agree fully from a purists PoV: pure OO means I get to redefine anything I want when I inherit, a la Smalltalk. In practice though I like to think that the re-use of my objects and class hierarchies is a critical part of their design. Just about anything I'm gonna have to override to make a simple mock will also be an issue for other implementers. So why not use the opportunity to abstract and refine the objects internal logic to make mocking the relevant functionality cleaner and more obvious? Create an internal "OnProcessComplete" method which can be overridden easily instead of mocking the whole "Process" command. Over-mocking is a major code smell and productivity thief. I find that having to manually set it up keeps me honest about cost/benefit, while also looking at an important design aspect that otherwise could be ignored.
- bluGill 9y agoI don't know C#, but I know in C++ I have been burned because I mocked a real class - but I didn't override some function and so my test ended up using a mix between the real class and the mocks leading to code that wasn't testing what I wanted it to. Often the tests pass, but there is a lurking bug.