Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have the same uneasy feeling when I write tests like this. It especially strikes me when I implement the function by copy'n'pasting the the expectations into the function body (this works when I use LeMock for mocking).</p> <p>But its ok. It happens. This test now documents and verifies how the system under test interacts with its dependencies, which is a good thing. There are other problems with this test though:</p> <ol> <li><p>It is testing too much at once. This test verifies that three dependencies are called correctly. If either of those dependencies change, this test will have to change. It would be better to have 3 separate tests, verifying each dependency is properly handled. Pass in a stub object for the dependencies you aren't testing (rather than a mock, as it will fail).</p></li> <li><p>Theres no verification on the parameters passed to the dependencies, so these tests are incomplete. </p></li> </ol> <p>In this sample I will use Moq as the mocking library. This test does not specify the behavior of all the dependencies, it only tests one call. It will also check that the passed in parameter is whats expected given the input, variations of input would justify separate tests.</p> <pre><code>public void AddEventSavesSnapshot(object eventSnaphot) { Mock&lt;IDocumentDao&gt; mockDocumentDao = new Mock&lt;IDocumentDao&gt;(); Mock&lt;IPatientSnapshotService&gt; mockPatientSnapshot = new Mock&lt;IPatientSnapshotService&gt;(); string eventSample = Some.String(); EventSection eventSection = new EventSection(eventSample); mockPatientSnapshot.Setup(r =&gt; r.SaveEventSnapshot(eventSample)); AddDocumentEventService sut = new AddDocumentEventService(); sut.DocumentDao = mockDocumentDao; sut.PatientSnapshotService = mockPatientSnapshot; sut.AddEvent(eventSection); mockPatientSnapshot.Verify(); } </code></pre> <p>Note that the unused dependencies that are passed in would only be needed in this test if AddEvent() might use them. Quite reasonably the class might have dependencies not related to this test not shown.</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