---
layout: post
title: Making mock tests less volatile
categories: []
tags:
- Design
- Unit Testing
status: publish
type: post
published: true
meta:
  _elasticsearch_indexed_on: '2008-09-12 21:21:00'
comments: true
---
<p>In a follow-up to <a href="/blogengine/post/2008/09/11/Mocks-can-be-your-friend-or-your-worst-nightmare.aspx">my previous post on mocking</a>, here is the same code re-factored so that it does not depend on the internal behavior of <em>GetAllEmployeesByCompanyId</em></p>  <p>&nbsp;</p>  <div style="font-size:8pt;width:96.83%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;height:283px;background-color:#f4f4f4;border-color:gray;border-style:solid;border-width:1px;margin:20px 0 10px;padding:4px;">   <div style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">     <pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;height:19px;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> [TestMethod]</pre><pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> GetAllEmployeesByCompanyId_When_Employees_Are_Marked_Returns_Non_Marked_Employees()</pre><pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> {</pre><pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     IEmployeeRepository employeeRepository = MockRepository.GenerateMock&lt;IEmployeeRepository&gt;();</pre><pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>&nbsp; </pre><pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     User user = <span style="color:#0000ff;">new</span> User { Id = Guid.NewGuid()};</pre><pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>&nbsp; </pre><pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>     Employee employee = <span style="color:#0000ff;">new</span> Employee {Id = Guid.NewGuid()};</pre><pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>&nbsp; </pre><pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>     Employee deletedEmployee = <span style="color:#0000ff;">new</span> Employee { Id = Guid.NewGuid(), DeletedBy = user};</pre><pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>&nbsp; </pre><pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>     employeeRepository.Stub(x =&gt; x.GetEmployeesByCompanyId&lt;Employee&gt;(Guid.Empty)).Return(<span style="color:#0000ff;">new</span> List&lt;Employee&gt;</pre><pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>                                                                                              {</pre><pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>                                                                                                  employee,</pre><pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>                                                                                                  deletedEmployee</pre><pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>                                                                                              });</pre><pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>&nbsp; </pre><pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span>     var EmployeeServices = <span style="color:#0000ff;">new</span> EmployeeServices(employeeRepository);</pre><pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span>&nbsp; </pre><pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  20:</span>     var employees = EmployeeServices.GetAllEmployeesBy
CompanyId(Guid.Empty);</pre><pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  21:</span>&nbsp; </pre><pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  22:</span>     Assert.AreEqual(1, employees.Count);</pre><pre style="font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  23:</span> }</pre></div></div><p>I&#039;ve done quite a bit of re-factoring, taking advantage of some of the C# 3.0 language features. I&#039;m also using RhinoMocks 3.5 which also makes use of extension methods. In general the test is much cleaner, less noise. In fact, apart from two &quot;strange&quot; calls (lines 4 and 12), you wouldn&#039;t even notice it&#039;s using mocks. </p><p>The important change is that its no longer brittle. If you recall correctly, from my previous post, the purpose of the test was to validate that when <em>GetAllEmployeesByCompanyId, </em>only records not marked for deletion were returned. It was not about interaction or the internal behavior of the call. As such, we&#039;ve removed this tight coupling to the actual implementation by </p><p><strong>a) Replacing the strict mock with a dynamic mock [Line 4]. <br /></strong>In version 3.5 of RhinoMocks, you no longer need to create a mock repository if you don&#039;t want to. As such, you have static methods on the <em>MockRepository </em>class that generate mocks for you. By default, the mock generated is Dynamic. Therefore, the call <em>MockRepository.GenerateMock</em> is a dynamic mock.</p><p>b) <strong>Removing the call to <em>VerifyAll. </em><br /></strong>Since we do not set any expectations, we do not really need or want to verify that any specific calls were made. Again, that&#039;s not the purpose of this particular test.</p><p>In line 12, we&#039;ve stubbed the call to <em>GetEmployeesByCompanyId </em>of <em>EmployeeRepository </em>so that it returns a list with two employees. If the repository call were to not return a value, we wouldn&#039;t even need to explicitly stub it. </p><p>As you can see, we are still using mocks, yet we are not making our test volatile to change. We are using mocks to mock out non existent functionality. This doesn&#039;t mean to say that we shouldn&#039;t use mocks for verifying behavior. But we should only do that if the purpose of the test is that. In this case it isn&#039;t. </p><p>RhinoMocks 3.5 is pretty cool. It allows a lot of the noise surrounding mocking to be eliminated in tests. Hopefully I&#039;ll blog more about it as I use this version more, but be sure to check it out (Don&#039;t be lazy...Just Google it!)</p>
