title: How to mock a method with an out parameter?
body: <p>I am using a library that uses out parameters in a function and I need to test my code using that function.</p>
  <p>So, attempting to have mocks come to my rescue here, via Moq which I've been using in the rest of the project.</p>
  <h1>Question</h1>
  <p>I know there's a wall of text below, so the question (in advance) is:</p>
  <ul>
  <li><strong>Per the lead below: Can Moq mock an item with a constructor that requires parameters that isn't normally called on its own?</strong></li>
  <li>Is this a problem with my test code? With the library? With the validation library?</li>
  <li>Am I utilizing Moq with out parameters?</li>
  <li>Where do I even start debugging this?</li>
  </ul>
  <h1>Update: Leads so Far</h1>
  <p>I'm thinking this is an issue on the mocking side with mocking the IXLRow interface. Normally it appears an XLRow is only instantiated from a workbook and never through <code>new XLRow()</code> -- is that a factor?</p>
  <p>The following test passes when (note: mocks):</p>
  <pre><code>   [Fact]
      public void TryGetValueCanReturnTrueForVieldWithAnInteger_WhenAccessingFromRow()
      {
          var workbook = new XLWorkbook();
          workbook.Worksheets.Add(&quot;TestWS&quot;);
          var wb = workbook.Worksheet(&quot;TestWS&quot;);
          wb.Cell(&quot;A1&quot;).Value = &quot;12345&quot;;
  
          // NOTE: Here we're referring to the row as part of an instantiated  
          //       workbook instead of Mocking it by itself
          int output;
          Assert.True(wb.Row(1).Cell(&quot;A&quot;).TryGetValue(out output));
      }
  </code></pre>
  <h2>The Code</h2>
  <p>Snippet of the method that gets a mock of a valid object():</p>
  <pre><code>// ...other code that sets up other parts of the row correctly
  int isAnyInt = 0; //I don't care about this value, only the true/false
  
  // set this to false to true to mimic a row being a legitimate integer
  mock.Setup(m =&gt; m.Cell(&quot;B&quot;).TryGetValue(out isAnyInt)).Returns(true);
  </code></pre>
  <p>xUnit test that tests the happy path -- Gets a mock of a valid row and then ensures it passes validation. <strong>NOTE: This test passes.</strong></p>
  <pre><code>    [Fact]
      public void Validate_GivenValidRow_ReturnsValid()
      {
          var mockRow = TestHelper.GetMockValidInvoiceDetailsWorksheetRow();
  
          var validationResult = new InvoiceDetailsWorksheetRowValidator().Validate(mockRow.Object);
          Assert.True(validationResult.IsValid);
      }
  </code></pre>
  <p>an xUnit test (basically, &quot;does the validator fail with a cell that isn't an integer?&quot;)
  <strong>NOTE: This test passes.</strong></p>
  <pre><code>    [Fact]
      public void Validate_GivenNonNumericClaimantID_ReturnsInvalid()
      {
          int outint = 0;
  
          // Get a mock of a valid row
          var mockRow = TestHelper.GetMockValidInvoiceDetailsWorksheetRow();
  
          // change the TryGetValue result to false
          mockRow.Setup(m =&gt; m.Cell(&quot;B&quot;).TryGetValue(out outint)).Returns(false);
  
          var validationResult = new InvoiceDetailsWorksheetRowValidator().Validate(mockRow.Object);
          Assert.False(validationResult.IsValid);
          Assert.Equal(&quot;ClaimantID column value is not a number.&quot;, validationResult.Errors.First().ErrorMessage);
      }
  </code></pre>
  <p>The validator (using FluentValidation):</p>
  <pre><code>public class InvoiceDetailsWorksheetRowValidator : AbstractValidator&lt;IXLRow&gt;
  {
      public InvoiceDetailsWorksheetRowValidator()
      {
          RuleFor(x =&gt; x.Cell(&quot;B&quot;))
              .Must(BeAnInt).WithMessage(&quot;ClaimantID column value is not a number.&quot;)
              .OverridePropertyName(&quot;ClaimantIDColumn&quot;);
  
      }
  
      private bool BeAnInt(IXLCell cellToCheck)
      {
          int result;
          var successful = cellToCheck.TryGetValue(out result);
          return successful;
      }
  }
  </code></pre>
  <p>For reference, the method from the library:</p>
  <pre><code>    public Boolean TryGetValue&lt;T&gt;(out T value)
      {
          var currValue = Value;
  
          if (currValue == null)
          {
              value = default(T);
              return true;
          }
  
          bool b;
          if (TryGetTimeSpanValue(out value, currValue, out b)) return b;
  
          if (TryGetRichStringValue(out value)) return true;
  
          if (TryGetStringValue(out value, currValue)) return true;
  
          var strValue = currValue.ToString();
          if (typeof(T) == typeof(bool)) return TryGetBasicValue&lt;T, bool&gt;(out value, strValue, bool.TryParse);
          if (typeof(T) == typeof(sbyte)) return TryGetBasicValue&lt;T, sbyte&gt;(out value, strValue, sbyte.TryParse);
          if (typeof(T) == typeof(byte)) return TryGetBasicValue&lt;T, byte&gt;(out value, strValue, byte.TryParse);
          if (typeof(T) == typeof(short)) return TryGetBasicValue&lt;T, short&gt;(out value, strValue, short.TryParse);
          if (typeof(T) == typeof(ushort)) return TryGetBasicValue&lt;T, ushort&gt;(out value, strValue, ushort.TryParse);
          if (typeof(T) == typeof(int)) return TryGetBasicValue&lt;T, int&gt;(out value, strValue, int.TryParse);
          if (typeof(T) == typeof(uint)) return TryGetBasicValue&lt;T, uint&gt;(out value, strValue, uint.TryParse);
          if (typeof(T) == typeof(long)) return TryGetBasicValue&lt;T, long&gt;(out value, strValue, long.TryParse);
          if (typeof(T) == typeof(ulong)) return TryGetBasicValue&lt;T, ulong&gt;(out value, strValue, ulong.TryParse);
          if (typeof(T) == typeof(float)) return TryGetBasicValue&lt;T, float&gt;(out value, strValue, float.TryParse);
          if (typeof(T) == typeof(double)) return TryGetBasicValue&lt;T, double&gt;(out value, strValue, double.TryParse);
          if (typeof(T) == typeof(decimal)) return TryGetBasicValue&lt;T, decimal&gt;(out value, strValue, decimal.TryParse);
  
          if (typeof(T) == typeof(XLHyperlink))
          {
              XLHyperlink tmp = GetHyperlink();
              if (tmp != null)
              {
                  value = (T)Convert.ChangeType(tmp, typeof(T));
                  return true;
              }
  
              value = default(T);
              return false;
          }
  
          try
          {
              value = (T)Convert.ChangeType(currValue, typeof(T));
              return true;
          }
          catch
          {
              value = default(T);
              return false;
          }
      }
  </code></pre>
  <h2>The Problem</h2>
  <p>The first test passes. But when I run this test, it fails:</p>
  <pre><code>   [Fact]
     public void Validate_GivenNonNumericInvoiceNumber_ReturnsInvalid()
      {
          int outint = 0; // I don't care about this value
  
          // Get a mock of a valid worksheet row
          var mockRow = TestHelper.GetMockValidInvoiceDetailsWorksheetRow();
  
          mockRow.Setup(m =&gt; m.Cell(&quot;E&quot;).TryGetValue(out outint)).Returns(false);
  
          // Validates &amp; asserts
          var validationResult = new InvoiceDetailsWorksheetRowValidator().Validate(mockRow.Object);
          Assert.False(validationResult.IsValid);
  
          // Placed here to ensure it's the only error message. This is where it fails.
          Assert.Equal(&quot;InvoiceNumber column value is not a number.&quot;,validationResult.Errors.First().ErrorMessage);
      }
  </code></pre>
  <p>But it doesn't fail because the validation hasn't been implemented -- it fails because the other item is invalid first, even though I'm returning it from getting a valid mock -- the same valid mock that passes tests otherwise.</p>
  <p>The message, exactly, is:</p>
  <blockquote>
  <p>Assert.Equal() Failure</p>
  <p>Position: First difference is at position 0</p>
  <p>Expected: InvoiceNumber column value is not a number.</p>
  <p>Actual:  ClaimantID column value is not a number.</p>
  </blockquote>
  <p>I would expect:</p>
  <ul>
  <li>It to work the same way the other test worked, or</li>
  <li>For the happy path to also fail.</li>
  </ul>
  <p>But when the happy path (e.g. valid mock) passes, but the test fails because the method is invalid (the same one that passes the same validation as part of the &quot;valid&quot; mock)...it leaves me completely confused.</p>
  <h2>For Reference</h2>
  <ul>
  <li>The library I'm using is <a href="https://closedxml.codeplex.com" rel="noreferrer">ClosedXML</a></li>
  <li>The validation library I'm using is <a href="https://fluentvalidation.codeplex.com" rel="noreferrer">FluentValidation</a></li>
  <li>I'm using <a href="https://xunit.codeplex.com" rel="noreferrer">xUnit.NET</a> to unit test.</li>
  </ul>