Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I test a method was called within a class under test?
    text
    copied!<p>I'll start by saying I am pretty new to unit testing and I'd like to start using a TDD approach, but for now am writing unit tests for some existing classes to verify their functionality in all cases.</p> <p>I've been able to test the majority of my code using NUnit and Rhino mocks without much trouble. However, I've been wondering about unit testing functions that end up calling a lot of other methods within the same class. I can't do something like </p> <pre><code>classUnderTest.AssertWasCalled(cut =&gt; cut.SomeMethod(someArgs)) </code></pre> <p>because the class under test isn't a fake. Furthermore, if a method I'm testing calls other methods in the class under test that in turn also call methods in the same class, I'm going to need to fake a ton of values just to test the "top level" method. Since I'm also unit testing all of these "sub methods", I should be able to assume that "SomeMethod" works as expected if it passes the unit test and not need to worry about the details of those lower-level methods.</p> <p>Here is some example code I've been working with to help illustrate my point (I've written a class to manage import/export of Excel files using NPOI):</p> <pre><code> public DataSet ExportExcelDocToDataSet(bool headerRowProvided) { DataSet ds = new DataSet(); for (int i = 0; i &lt; currentWorkbook.NumberOfSheets; i++) { ISheet tmpSheet = currentWorkbook.GetSheetAt(i); if (tmpSheet.PhysicalNumberOfRows == 0) { continue; } DataTable dt = GetDataTableFromExcelSheet(headerRowProvided, ds, tmpSheet); if (dt.Rows.Count &gt; 0) { AddNonEmptyTableToDataSet(ds, dt); } } return ds; } public DataTable GetDataTableFromExcelSheet(bool headerRowProvided, DataSet ds, ISheet tmpSheet) { DataTable dt = new DataTable(); for (int sheetRowIndex = 0; sheetRowIndex &lt;= tmpSheet.LastRowNum; sheetRowIndex++) { DataRow dataRow = GetDataRowFromExcelRow(dt, tmpSheet, headerRowProvided, sheetRowIndex); if (dataRow != null &amp;&amp; dataRow.ItemArray.Count&lt;object&gt;(obj =&gt; obj != DBNull.Value) &gt; 0) { dt.Rows.Add(dataRow); } } return dt; } ... </code></pre> <p>You can see that <strong>ExportExcelDocToDataSet</strong> (my "top-level" method in this case) calls <strong>GetDataTableFromExcelSheet</strong> which calls <strong>GetDataRowFromExcelRow</strong>, which calls a couple of other methods that are defined within this same class.</p> <p>So what is the recommended strategy for refactoring this code to make it more unit testable without having to stub values called by submethods? Is there a way to fake method calls within the class under test?</p> <p>Thanks in advance for any help or advice!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload