{"id":376,"date":"2010-03-30T20:47:09","date_gmt":"2010-03-30T19:47:09","guid":{"rendered":"http:\/\/blog.eweibel.net\/?p=376"},"modified":"2011-07-14T18:14:59","modified_gmt":"2011-07-14T16:14:59","slug":"mocking-frameworks-in-net","status":"publish","type":"post","link":"https:\/\/blog.eweibel.net\/?p=376","title":{"rendered":"Mocking frameworks in .Net"},"content":{"rendered":"<p>A few month ago I played with some mocking frameworks in .Net. There are already some comparisons available (<a href=\"http:\/\/codevanced.net\/post\/Mocking-frameworks-comparison.aspx\">here<\/a>, <a href=\"http:\/\/www.phpvs.net\/2009\/04\/25\/net-mocking-frameworks-capability-comparison\/\">here<\/a> or <a href=\"http:\/\/code.google.com\/p\/mocking-frameworks-compare\/\">here<\/a>). In this blog post I want to show which frameworks are available and which one fits best for agile development.<\/p>\n<p>You could download the source code from <a href=\"http:\/\/github.com\/pweibel\/DotNetMockingFrameworksDemo\">github.com<\/a>.<\/p>\n<p><strong>Software under Test (SUT)<\/strong><\/p>\n<p>To demonstrate and evaluate all the different mocking framework I had to create a scenario where it is appropriate to use a mocking framework. So I chose an user entity which I want to validate and in a successful case also to persist in a database.<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:1c54bba7-1211-4e44-a7bb-fb0296f30780\" class=\"wlWriterSmartContent\">\n<pre class=\"brush: c#\">namespace Mocking\n{\n\tpublic class User\n\t{\n\t\tpublic IUserGateway Gateway\n\t\t{\n\t\t\tget; set;\n\t\t}\n\n\t\tpublic User()\n\t\t{\n\t\t\tthis.Gateway = new UserGateway();\n\t\t}\n\n\t\tpublic User(IUserGateway gateway)\n\t\t{\n\t\t\tthis.Gateway = gateway;\n\t\t}\n\n\t\tpublic bool Persist(IUserValidator validator)\n\t\t{\n\t\t\tbool bValid = validator.Validate(this);\n\n\t\t\tif(bValid) bValid = this.Gateway.Persist(this);\n\n\t\t\treturn bValid;\n\t\t}\n\t}\n}<\/pre>\n<\/div>\n<p>This is the User entity class which implements the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Active_record_pattern\">active record pattern<\/a>. As you see, there are three <a href=\"http:\/\/martinfowler.com\/articles\/injection.html\">injection patterns<\/a> realized, the constructor injection and setter injection for the gateway and the parameter injection for the validator.<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:34ee3999-b915-4950-b159-d53dd3b5d876\" class=\"wlWriterSmartContent\">\n<pre class=\"brush: c#\">namespace Mocking\n{\n\tpublic class UserGateway : IUserGateway\n\t{\n\t\tpublic virtual bool Persist(User user)\n\t\t{\n\t\t\treturn true;\n\t\t}\n\t}\n}<\/pre>\n<\/div>\n<p>The UserGateway class is responsible to persist the entity. In my scenario it is just a dummy, because I don\u2019t want to setup a database for this demo.<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:b59fb655-28ab-4e61-a554-809a0029283b\" class=\"wlWriterSmartContent\">\n<pre class=\"brush: c#\">namespace Mocking\n{\n\tpublic class UserValidator : IUserValidator\n\t{\n\t\tpublic bool Validate(User user)\n\t\t{\n\t\t\tif(user == null) return false;\n\n\t\t\treturn true;\n\t\t}\n\t}\n}<\/pre>\n<\/div>\n<p>The validator is also more or less just a dummy for this demo.<\/p>\n<p>Both interfaces, IUserGateway and IUserValidator, has the same methods, properties as the classes which implement them.<\/p>\n<p><strong>NMock2<\/strong><\/p>\n<p>One of the first mocking frameworks in .Net which I was looking at was <a href=\"http:\/\/www.nmock.org\/\">NMock2<\/a>. Specially the <a href=\"http:\/\/www.nmock.org\/cheatsheet.html\">cheat sheet<\/a> make it really easy to learn this framework.<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:0246b749-6eb5-49f6-a1ac-e99c73a98299\" class=\"wlWriterSmartContent\">\n<pre class=\"brush: c#\">namespace MockingTest\n{\n\t[TestClass]\n\tpublic class NMock2Test\n\t{\n\t\t[TestMethod]\n\t\tpublic void TestPersist()\n\t\t{\n\t\t\tMockery mocks = new Mockery();\n\n\t\t\t\/\/Create mocks\n\t\t\tIUserGateway mockGateway = mocks.NewMock&lt;IUserGateway&gt;();\n\t\t\tIUserValidator mockValidator = mocks.NewMock&lt;IUserValidator&gt;();\n\n\t\t\t\/\/Create user\n\t\t\tUser user = new User();\n\n\t\t\t\/\/Expectations\n\t\t\tusing(mocks.Ordered)\n\t\t\t{\n\t\t\t\tExpect.Once.On(mockValidator).Method(&quot;Validate&quot;).With(user).\nWill(Return.Value(true));\n\t\t\t\tExpect.Once.On(mockGateway).Method(&quot;Persist&quot;).With(user).\nWill(Return.Value(true));\n\t\t\t}\n\n\t\t\t\/\/Assign Gateway\n\t\t\tuser.Gateway = mockGateway;\n\n\t\t\t\/\/Test method\n\t\t\tAssert.AreEqual(true, user.Persist(mockValidator));\n\n\t\t\tmocks.VerifyAllExpectationsHaveBeenMet();\n\t\t}\n\t}\n}<\/pre>\n<\/div>\n<p>You could use the <a href=\"http:\/\/blog.eweibel.net\/?p=200\">AAA-Syntax<\/a> (Arrange-Act-Assert). Also more extended features like the ordered calls with the using syntax is very nice.<\/p>\n<p>But, there is are also negative points: NMock2 uses \u201cmagic strings\u201d. In your Expectations you have to declare the called method with a strings, which makes your tests very brittle when your refactor your code. For example if you have to rename a method, even the compiler could not help you. An other negative point is, that you can only mock interfaces. When you have to mock a class, you have to change your design by adding an interface just for mocking.<\/p>\n<p><strong>NMock3<\/strong><\/p>\n<p>Recently I discover, that there is already a follower of NMock2 on codeplex: <a href=\"http:\/\/nmock3.codeplex.com\/\">NMock3<\/a>.<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:f61286f3-7728-4745-b151-9f43e9ce5c7d\" class=\"wlWriterSmartContent\">\n<pre class=\"brush: c#\">namespace NMock3Test\n{\n    [TestClass]\n    public class NMock3Test\n    {\n        [TestMethod]\n        public void TestPersist()\n        {\n            MockFactory factory = new MockFactory();\n\n            \/\/Create mocks\n            Mock&lt;IUserGateway&gt; mockGateway = factory.CreateMock&lt;IUserGateway&gt;();\n            Mock&lt;IUserValidator&gt; mockValidator = factory.CreateMock&lt;IUserValidator&gt;();\n\n            \/\/Create user\n            User user = new User();\n\n            \/\/Expectations\n            using(factory.Ordered)\n            {\n                mockValidator.Expects.One.MethodWith(m =&gt; m.Validate(user)).WillReturn(true);\n                mockGateway.Expects.One.MethodWith(m =&gt; m.Persist(user)).WillReturn(true);\n            }\n\n            \/\/Assign gateway\n            user.Gateway = mockGateway.MockObject;\n\n            \/\/Test method\n            Assert.AreEqual(true, user.Persist(mockValidator.MockObject));\n\n            factory.VerifyAllExpectationsHaveBeenMet();\n        }\n    }\n}<\/pre>\n<\/div>\n<p>The migration is quite easy: Replace the reference from your test project from the NMock2.dll with a new reference to the NMock3.dll. After that you could use the new approach with the MockFactory and the lambda expressions in your Expectations.<\/p>\n<p>NMock3 introduce type safety and readiness for refactorings combined with the easiness of NMock2. They also keep the tradition of a good cheat sheet.<\/p>\n<p>With NMock3 I couldn\u2019t mock a class (even when I followed the exceptions\u2026&quot;). The creation of stubs should be possible. The project is still a young one and so the documentation isn\u2019t that good as it should be. But NMock3 remain an interesting mocking framework.<\/p>\n<p><strong>Simple.Mocking<\/strong><\/p>\n<p>When I looked for mocking frameworks I also found this new and not very well known framework <a href=\"http:\/\/simpledotnet.codeplex.com\/\">Simple.Mocking<\/a> on codeplex. Its also avoid \u201cmagic strings\u201d and you could also use the AAA-Syntax. Simple.Mocking has only one committer, Mikael Waltersson.<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:1de5b772-4e6d-4499-a0d8-c6bbe0feab0b\" class=\"wlWriterSmartContent\">\n<pre class=\"brush: c#\">namespace MockingTest\n{\n    [TestClass]\n    public class SimpleNetTest\n    {\n        [TestMethod]\n        public void TestPersist()\n        {\n            \/\/Create mocks\n            var expectationScope = new ExpectationScope();\n            IUserGateway mockGateway = Mock.Interface&lt;IUserGateway&gt;(expectationScope);\n            IUserValidator mockValidator = Mock.Interface&lt;IUserValidator&gt;(expectationScope);\n\n            \/\/Create user\n            User user = new User();\n\n            \/\/Expectations\n            Expect.Once.MethodCall(() =&gt; mockValidator.Validate(user)).Returns(true);\n            Expect.Once.MethodCall(() =&gt; mockGateway.Persist(user)).Returns(true);\n\n            \/\/Assign gateway\n            user.Gateway = mockGateway;\n\n            \/\/Test method\n            Assert.AreEqual(true, user.Persist(mockValidator));\n\n            AssertExpectations.IsMetFor(expectationScope);\n        }\n    }\n}<\/pre>\n<\/div>\n<p>The ExpectationsScope is used to avoid to call AssertExpectations.IsMetFor for each mock. This construct is special and I\u2019m (currently) not a big fan of it.<\/p>\n<p>With Simple.Mocking you could just mocking interfaces and delegates, you couldn\u2019t mock classes.<\/p>\n<p><strong>Rhino.Mocks<\/strong><\/p>\n<p><a href=\"http:\/\/www.ayende.com\/projects\/rhino-mocks.aspx\">Rhino.Mocks<\/a> is one of the famous .Net mocking frameworks. As Simple.Mocking there is also only one committer to this framework, Oren Eini (alias Ayende Rahien).<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:a76135a5-0f97-48ec-8467-dfc526911821\" class=\"wlWriterSmartContent\">\n<pre class=\"brush: c#\">namespace MockingTest\n{\n\t[TestClass]\n\tpublic class RhinoMockTest\n\t{\n\t\t[TestMethod]\n\t\tpublic void TestPersistWithRecordingReplay()\n\t\t{\n\t\t\tMockRepository mocks = new MockRepository();\n\n\t\t\tIUserGateway gateway = mocks.StrictMock&lt;IUserGateway&gt;();\n\t\t\tIUserValidator validator = mocks.StrictMock&lt;IUserValidator&gt;();\n\n\t\t\t\/\/Create user\n\t\t\tUser user = new User();\n\n\t\t\t\/\/Expectations\n\t\t\tusing(mocks.Ordered())\n\t\t\t{\n\t\t\t\tExpect.Call(validator.Validate(user)).Return(true);\n\t\t\t\tExpect.Call(gateway.Persist(user)).Return(true);\n\t\t\t}\n\n\t\t\t\/\/Stop recording, start replay\n\t\t\tmocks.ReplayAll();\n\n\t\t\t\/\/Assign gateway\n\t\t\tuser.Gateway = gateway;\n\n\t\t\t\/\/Test method\n\t\t\tAssert.AreEqual(true, user.Persist(validator));\n\n\t\t\tmocks.VerifyAll();\n\t\t}\n\n\t\t[TestMethod]\n\t\tpublic void TestPersistWithArrangeActAssert()\n\t\t{\n\t\t\tvar gateway = MockRepository.GenerateMock&lt;IUserGateway&gt;();\n\t\t\tvar validator = MockRepository.GenerateMock&lt;IUserValidator&gt;();\n\n\t\t\t\/\/Create user\n\t\t\tUser user = new User();\n\n\t\t\t\/\/Expectations\n\t\t\tvalidator.Expect(x =&gt; x.Validate(user)).Return(true);\n\t\t\tgateway.Expect(x =&gt; x.Persist(user)).Return(true);\n\n\t\t\t\/\/Assign gateway\n\t\t\tuser.Gateway = gateway;\n\n\t\t\t\/\/Test method\n\t\t\tAssert.AreEqual(true, user.Persist(validator));\n\n\t\t\t\/\/Asserts\n\t\t\tvalidator.VerifyAllExpectations();\n\t\t\tgateway.VerifyAllExpectations();\n\t\t}\n\t}\n}<\/pre>\n<\/div>\n<p>Rhino.Mocks supports two ways to use the framework: the record-replay and the AAA-syntax. My favorite is clearly the AAA-syntax, because there is much less code needed (reduced <a href=\"http:\/\/en.wikipedia.org\/wiki\/Accidental_complexity\">accidental complexity<\/a>). Rhino.Mocks uses also Lambda-Expressions to avoid \u201cmagic strings\u201d, so refactoring shouldn\u2019t be a problem.<\/p>\n<p><strong>Moq<\/strong><\/p>\n<p><a href=\"http:\/\/code.google.com\/p\/moq\/\">Moq<\/a> is another mocking framework which takes advantage of the .Net 3.0 language features like lambda expressions. So Moq doesn\u2019t use \u201cmagic strings\u201d and it enables also the AAA-syntax. There are two ways how to create mocks. You could create mocks directly with the Mock class or you could use the MockFactory class. For both scenarios I created a test.<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:ab7228d9-8fc2-4966-9225-74e332db81b5\" class=\"wlWriterSmartContent\">\n<pre class=\"brush: c#\">namespace MockingTest\n{\n\t[TestClass]\n\tpublic class MoqTest\n\t{\n\t\t[TestMethod]\n\t\tpublic void TestPersist()\n\t\t{\n\t\t\tvar gateway = new Mock&lt;IUserGateway&gt;();\n\t\t\tvar validator = new Mock&lt;IUserValidator&gt;();\n\n\t\t\tUser user = new User();\n\n\t\t\t\/\/Expectations\n\t\t\tvalidator.Setup(x =&gt; x.Validate(user)).Returns(true).AtMostOnce();\n\t\t\tgateway.Setup(x =&gt; x.Persist(user)).Returns(true).AtMostOnce();\n\n\t\t\t\/\/Assign gateway\n\t\t\tuser.Gateway = gateway.Object;\n\n\t\t\t\/\/Test method\n\t\t\tAssert.AreEqual(true, user.Persist(validator.Object));\n\n\t\t\tvalidator.VerifyAll();\n\t\t\tgateway.VerifyAll();\n\t\t}\n\n\t\t[TestMethod]\n\t\tpublic void TestPersistWithFactory()\n\t\t{\n\t\t\tMockFactory factory = new MockFactory(MockBehavior.Strict);\n\n\t\t\t\/\/Class works, but methods have to be virtual -&gt; not nice\n\t\t\tvar mockGateway = factory.Create&lt;UserGateway&gt;();\n\t\t\tvar mockValidator = factory.Create&lt;IUserValidator&gt;();\n\n\t\t\tUser user = new User();\n\n\t\t\t\/\/Expectations\n\t\t\tmockValidator.Setup(x =&gt; x.Validate(user)).Returns(true).AtMostOnce();\n\t\t\tmockGateway.Setup(x =&gt; x.Persist(user)).Returns(true).AtMostOnce();\n\n\t\t\t\/\/Assign gateway\n\t\t\tuser.Gateway = mockGateway.Object;\n\n\t\t\t\/\/Test method\n\t\t\tAssert.AreEqual(true, user.Persist(mockValidator.Object));\n\n\t\t\tfactory.VerifyAll();\n\t\t}\n\t}\n}<\/pre>\n<\/div>\n<p>I prefer the MockFactory approach, because you don\u2019t need to verify each mock (its reduce the test code which is a good thing). Be sure, that in this scenario you don\u2019t forget the parameter MockBehavior.Strict which makes your mock objects real mocks. Without this parameter they are more like Stubs, which don\u2019t fail and just return a default value.<\/p>\n<p>But there is a little problem: If you want to mock a class, then all the methods have to be virtual, but Moq has no problem to mock an interface.<\/p>\n<p><strong>Typemock Isolator<\/strong><\/p>\n<p>Unfortunately <a href=\"http:\/\/www.typemock.com\/\">Typemock Isolator<\/a> isn\u2019t free, but you could get a trial to try this great mocking framework. As you could see in the TestPersistWithReflectiveMocking Typemock Isolator could mock classes, also non-virtual methods. Great. It also doesn\u2019t use \u201cmagic strings\u201d. But wait, you could not mock interfaces? With the new AAA-syntax it is possible and it looks very nice.<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:3b48fb6f-d07b-4ec0-b9b0-33a41604444f\" class=\"wlWriterSmartContent\">\n<pre class=\"brush: c#\">namespace MockingTest\n{\n\t[TestClass]\n\tpublic class TypeMockTest\n\t{\n\t    [TestMethod]\n\t    public void TestPersistWithNaturalMocking()\n\t    {\n\t        MockManager.Init();\n\n\t        User user = new User();\n\n\t        \/\/ Natural mocking\n\t        using(RecordExpectations recorder = new RecordExpectations())\n\t        {\n\t            IUserGateway mockGateway = new UserGateway();\n\t            IUserValidator mockValidator = new UserValidator();\n\t\t\t\t\n\t            \/\/Expectations\n\t            recorder.ExpectAndReturn(mockValidator.Validate(user), true);\n\t            recorder.ExpectAndReturn(mockGateway.Persist(user), true);\n\t        }\n\n\t        \/\/Create instances\n\t        IUserGateway gateway = new UserGateway();\n\t        IUserValidator validator = new UserValidator();\n\t\t\t\n\t        \/\/Assign gateway\n\t        user.Gateway = gateway;\n\n\t        \/\/Method\n\t        Assert.AreEqual(true, user.Persist(validator));\n\n\t        MockManager.Verify();\n\t    }\n\t\t\n\t    [TestMethod]\n\t    public void TestPersistWithReflectiveMocking()\n\t    {\n\t        MockManager.Init();\n\n\t        \/\/Create mocks\n\t        Mock mockGateway = MockManager.Mock&lt;UserGateway&gt;();\n\t        Mock mockValidator = MockManager.Mock&lt;UserValidator&gt;();\n\n\t        \/\/Create user\n\t        UserGateway gateway = new UserGateway();\n\t        IUserValidator validator = new UserValidator();\n\t        User user = new User(gateway);\n\n\t        \/\/Expectations\n\t        mockValidator.ExpectAndReturn(&quot;Validate&quot;, true).Args(user);\n\t        mockGateway.ExpectAndReturn(&quot;Persist&quot;, true).Args(user);\n\n\t        \/\/Method\n\t        Assert.AreEqual(true, user.Persist(validator));\n\n\t        MockManager.Verify();\n\t    }\n\n\t\t[TestMethod]\n\t\t[Isolated]\n\t\tpublic void TestPersistWithAAA()\n\t\t{\n\t\t\t\/\/Arrange\n\t\t\t\/\/Create mocks\n\t\t\tUserGateway gateway = Isolate.Fake.Instance&lt;UserGateway&gt;();\n\t\t\tIUserValidator validator = Isolate.Fake.Instance&lt;IUserValidator&gt;();\n            \/\/Create user\n\t\t\tUser user = new User(gateway);\n            \/\/Expectations\n\t\t\tIsolate.WhenCalled(() =&gt; validator.Validate(user)).WillReturn(true);\n\t\t\tIsolate.WhenCalled(() =&gt; gateway.Persist(user)).WillReturn(true);\n\n\t\t\t\/\/Act\n\t\t\tbool bPersist = user.Persist(validator);\n\n\t\t\t\/\/Assert\n\t\t\tAssert.AreEqual(true, bPersist);\n\t\t}\n\t}\n}<\/pre>\n<\/div>\n<p>Typemock Isolator also supports the record-replay approach (demonstrated in the TestPersistWithNaturalMocking test method). But as you already know, I prefer the AAA-syntax.<\/p>\n<p>The question about Typemock Isolator is, if it is worth to pay for a mocking framework if there are so much other free opportunities?&#160; Typemock Isolator is very powerful and for some companies it is very important to get support and also guarantee that the product will be continued for example ten years. In such a scenario Typemock Isolator would be definitely an option. In reaction of such scenarios, also other providers of mocking frameworks offer commercial support for their frameworks, for example <a href=\"http:\/\/www.ayende.com\/projects\/rhino-mocks\/commercial-support.aspx\">Rhino.Mocks<\/a>.<\/p>\n<p><strong>The undocumented &#8211; NUnit Mocking<\/strong><\/p>\n<p>It is not very known that there exists also in <a href=\"http:\/\/www.nunit.org\/\">NUnit<\/a> a mocking framework. It is possible to mock interfaces and classes if the inherit from MarshalByRefObject.<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:ea5ac195-96f9-4d3e-9733-62fa4ed553cc\" class=\"wlWriterSmartContent\">\n<pre class=\"brush: c#\">namespace MockingTest\n{\n\t[TestClass]\n\tpublic class NUnitMockTest\n\t{\n\t\t[TestMethod]\n\t\tpublic void TestPersist()\n\t\t{\n\t\t\t\/\/Gateway\n\t\t\tDynamicMock mockGateway = new DynamicMock(typeof(IUserGateway));\n\t\t\tIUserGateway gateway = (IUserGateway) mockGateway.MockInstance;\n\t\t\t\n\t\t\t\/\/Validator\n\t\t\tDynamicMock mockValidator = new DynamicMock(typeof(IUserValidator));\n\t\t\tIUserValidator validator = (IUserValidator)mockValidator.MockInstance;\n\n\t\t\t\/\/User\n\t\t\tUser user = new User(gateway);\n\n\t\t\t\/\/Expectations\n\t\t\tmockValidator.ExpectAndReturn(&quot;Validate&quot;, true, user);\n\t\t\tmockGateway.ExpectAndReturn(&quot;Persist&quot;, true, user);\n\n\t\t\tAssert.AreEqual(true, user.Persist(validator));\n\t\t\tmockValidator.Verify();\n\t\t\tmockGateway.Verify();\n\t\t}\n\t}\n}<\/pre>\n<\/div>\n<p>The mocking framework is easy to use and if you already use NUnit, then you don\u2019t need an additional framework.<\/p>\n<p>As I know, it isn\u2019t official and there is no documentation. You have also to use \u201cmagic strings\u201d for methods and the limitation for classes also reduce the benefit.<\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p>For a modern mocking framework it is essential that its enable refactoring by not using \u201cmagic strings\u201d. It is also essential for me that it allows to use the AAA-syntax. A third essential thing is that it allows you to mock classes. You don\u2019t want to create interface just for mocking, for example for your domain model. Avoid accidental complexity is very important, so you shouldn\u2019t add unnecessary code just for mocking. That doesn\u2019t mean that \u201cdesign for testability\u201d is wrong, \u201cdesign for testability\u201d means, that you design your code in a way, that you have the possibility to test your code, for example you avoid huge classes (<a href=\"http:\/\/de.wikipedia.org\/wiki\/God_object\">god object<\/a> anti-pattern) with a lot of private methods or you avoid creation of instances in methods (unable to use dependency injection).<\/p>\n<p>So, which framework fulfill all the requirements? NMock3, which inherit the easiness of NMock2 combined with the elimination of the \u201cmagic strings\u201d? Or Typemock Isolator which also eliminate the \u201cmagic strings\u201d and could even mock classes?<\/p>\n<p>For me, there isn\u2019t currently a clear winner visible, maybe Typemock makes the race. But there are frameworks which I would no longer use, as NMock2 or NUnit mocking. They don\u2019t fulfill my requirements clearly. That doesn\u2019t mean they aren\u2019t suitable for your scenarios.<\/p>\n<p>Also to be clear: There are other topics that should be considered: mocking events, Stubs, etc. The list of the mocking frameworks isn\u2019t complete, for example <a href=\"http:\/\/research.microsoft.com\/en-us\/projects\/moles\/\">Moles<\/a> is a framework which could be interesting too.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few month ago I played with some mocking frameworks in .Net. There are already some comparisons available (here, here or here). In this blog post I want to show which frameworks are available and which one fits best for agile development. You could download the source code from github.com. Software under Test (SUT) To demonstrate and evaluate all the different mocking framework I had to create a scenario where it is appropriate to use a mocking framework. So I&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/blog.eweibel.net\/?p=376\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[13,5,10],"tags":[],"class_list":["post-376","post","type-post","status-publish","format-standard","hentry","category-net","category-good-practices","category-testing"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/plOV9-64","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":718,"url":"https:\/\/blog.eweibel.net\/?p=718","url_meta":{"origin":376,"position":0},"title":"Do frameworks kill design skills?","author":"Patrick","date":"8. Feb 2011","format":false,"excerpt":"Software design is one of the most important skills a software engineer should have. But what is software design exactly? If you search for a definition you find something like this: Software design is a process of problem-solving and planning for a software solution. After the purpose and specifications of\u2026","rel":"","context":"In &quot;Design patterns&quot;","block_context":{"text":"Design patterns","link":"https:\/\/blog.eweibel.net\/?cat=3"},"img":{"alt_text":"image","src":"https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/image_thumb11.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1108,"url":"https:\/\/blog.eweibel.net\/?p=1108","url_meta":{"origin":376,"position":1},"title":"Know your warm-up","author":"Patrick","date":"31. Oct 2011","format":false,"excerpt":"I was this month in Berlin at the NHL match between the LA Kings and the Buffalo Sables. Half an hour before the game you could watch both teams doing a warm up session (see photo). They practice the techniques, moves and collaboration which they will use during the game.\u2026","rel":"","context":"In &quot;Good practices&quot;","block_context":{"text":"Good practices","link":"https:\/\/blog.eweibel.net\/?cat=5"},"img":{"alt_text":"NHL in Berlin (LA Kings vs. Buffalo Sabres)","src":"https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/Berlin-012_thumb1.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":137,"url":"https:\/\/blog.eweibel.net\/?p=137","url_meta":{"origin":376,"position":2},"title":"Registered the domain alpha-geek.ch","author":"Patrick","date":"8. May 2009","format":false,"excerpt":"I read recently the article of martin fowler about alpha-geek again. Then I checked various links which contains the keyword alpha geek. I was surprised that the domain alpha-geek.ch was free, so I couldn't resist to register it for me. Martin writes about alpha geeks: \"Alpha geeks are typically experimenting\u2026","rel":"","context":"In &quot;Private&quot;","block_context":{"text":"Private","link":"https:\/\/blog.eweibel.net\/?cat=9"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":160,"url":"https:\/\/blog.eweibel.net\/?p=160","url_meta":{"origin":376,"position":3},"title":"When to use stored procedures","author":"Patrick","date":"13. May 2009","format":false,"excerpt":"Recently I discussed with a colleague when to use stored procedures. As exptected it was quite a religious conversation. A few days later I found the following screencast: The Pros and Cons of Stored Procedures Based on the discussion and the screencast I tried to summarize my Pros and Cons:\u2026","rel":"","context":"In &quot;Good practices&quot;","block_context":{"text":"Good practices","link":"https:\/\/blog.eweibel.net\/?cat=5"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1213,"url":"https:\/\/blog.eweibel.net\/?p=1213","url_meta":{"origin":376,"position":4},"title":"Quality isn&rsquo;t a tool&ndash;You can&rsquo;t install it!","author":"Patrick","date":"20. Jun 2012","format":false,"excerpt":"Did you ask yourself why a team in an organization produces very good software quality and another team in the same organization just struggles to get things done and those things are in really bad quality? Interesting is also that for both teams exists the same rules (methologies, procedures, tools,\u2026","rel":"","context":"In &quot;Agile&quot;","block_context":{"text":"Agile","link":"https:\/\/blog.eweibel.net\/?cat=17"},"img":{"alt_text":"time, quality and money concept","src":"https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/Fotolia_36622856_S_thumb.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":677,"url":"https:\/\/blog.eweibel.net\/?p=677","url_meta":{"origin":376,"position":5},"title":"Round-up of a data centric architecture","author":"Patrick","date":"11. Apr 2010","format":false,"excerpt":"In my last big project we had to use a data centric architecture. There was a learning curve which architecture was the most appropriate one. The result is visible in the picture bellow: Lets explaining the diagram. The data (or state) is managed by the database layer and the common\u2026","rel":"","context":"In &quot;Software architecture&quot;","block_context":{"text":"Software architecture","link":"https:\/\/blog.eweibel.net\/?cat=4"},"img":{"alt_text":"Architektur","src":"https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/Architektur_thumb.jpg?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/Architektur_thumb.jpg?resize=350%2C200 1x, https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/Architektur_thumb.jpg?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/posts\/376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=376"}],"version-history":[{"count":35,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/posts\/376\/revisions"}],"predecessor-version":[{"id":1070,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/posts\/376\/revisions\/1070"}],"wp:attachment":[{"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}