Note that there are some explanatory texts on larger screens.

plurals
  1. POProper Approach to Unit testing a "complex" application service
    text
    copied!<p>I have an application service (ie. large method) responsible for coordinating the interaction between several business objects. Essentially it takes a DTO from one system which contains customer information and an invoice, and translates it and imports it into a different system based on various business rules.</p> <pre><code>public void ProcessQueuedData() { var queuedItems = _importServiceDAL.LoadQueuedData(); foreach (var queuedItem in queuedItems) { var itemType = GetQueuedImportItemType(queuedItem); if (itemType == QueuedImportItemType.Invoice) { var account = _accountDAL.GetAccountByAssocNo(queuedItem.Assoc); int agentAccountID; if (!account.IsValid) { agentAccountId = _accountDAL.AddAccount(queuedItem.Assoc); } else { agentAccountId = account.AgentAccountID; } /// Do additional processing TBD } } } </code></pre> <p>For the unit tests, is it correct to assume the entire process within the service should be tested on a granular step by step basis, similar to the following?</p> <p><strong>ImportService_ProcessQueuedData_CallsDataAccessLayer_ToLoadQueue</strong></p> <p><strong>ImportService_ProcessQueuedData_WithQueuedItemToProccess_ChecksIfAccountExists</strong></p> <p><strong>ImportService_ProcessQueuedData_WithInvoice_CallsDALToCreateAccountIfOneDoesNotExist</strong></p> <p>Here's a typical test:</p> <pre><code> [TestMethod()] public void ImportService_ProcessQueuedData_WithInvoice_CallsDALToCheckIfAgentAccountExists() { var accountDAL = MockRepository.GenerateStub&lt;IAccountDAL&gt;(); var importServiceDAL = MockRepository.GenerateStub&lt;IImportServiceDAL&gt;(); importServiceDAL.Stub(x =&gt; x.LoadQueuedData()) .Return(GetQueuedImportItemsWithInvoice()); accountDAL.Stub(x =&gt; x.GetAccountByAssocNo("FFFFF")) .IgnoreArguments() .Return(new Account() { AgentAccountId = 0 }); var importSvc = new ImportService(accountDAL, importServiceDAL); importSvc.ProcessQueuedData(); accountDAL.AssertWasCalled(a =&gt; a.GetAccountByAssocNo("FFFFF"), o =&gt; o.IgnoreArguments()); accountDAL.VerifyAllExpectations(); } </code></pre> <p>My problem is that I end up doing so much setup in each of these tests it is becoming brittle. Is this the correct approach, and if so what are some pointers to avoid duplicating all of this setup within each granular test? </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