Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your approach is good, however I would adjust few things. I have made few changes to your test and mocking components which you can see below.</p> <p><strong>Unit Test</strong></p> <p>You method under test within your Unit Test (see below), does not really match with the method which you have defined in your question.</p> <p>Unit test method call:</p> <pre><code>SubmissionVersion.DeleteNote(repo.Object, subVersion.Object, note.Id.Value); </code></pre> <p>Method under test:</p> <pre><code>public static SubmissionVersion DeleteNote (IRepository repository, SubmissionVersion version, Guid noteId) </code></pre> <p>I assume the above method is part of another class, I called it as <strong>NotesHelper</strong> - Not the ideal name for repository calls, but it is just to get the compilation working.</p> <p>I would personally separate out your Unit test into 2 separate Unit tests. One to verify whether the required methods being called, and the other is to verify whether the notes have been removed from the collection.</p> <p>Also instead of creating a mocked SubmissionVersion, I created a fakeSubmissionVersion. This is because the routines within SubmissionVersion may not be mockable. You can also do a state based testing (preferred) to ensure the version.Notes.Remove(note); has been called.</p> <pre><code>[Fact] public void DeleteNote_Deletion_VerifyExpectedMethodsInvokecOnlyOnce() { // Arrange var note = new Note { Id = Guid.NewGuid() }; var fakeSubmissionVersion = new SubmissionVersion() { Notes = new List&lt;Note&gt;() }; var repo = new Mock&lt;IRepository&gt;(); repo.Setup(x =&gt; x.GetById&lt;Note&gt;(It.IsAny&lt;Guid&gt;())).Returns(note); // Act NotesHelper.DeleteNote(repo.Object, fakeSubmissionVersion, note.Id.Value); // Assert repo.Verify(x =&gt; x.GetById&lt;Note&gt;(note.Id), Times.Once()); repo.Verify(x =&gt; x.Save(fakeSubmissionVersion), Times.Once()); repo.Verify(x =&gt; x.Delete(note), Times.Once()); } </code></pre> <p>The above test can be further improved by defining each Verification in its own test. </p> <pre><code>[Fact] public void DeleteNote_Deletion_VerifyDeleteMethodCalledOnlyOnce() </code></pre> <p>This way if one the test fails we would know exactly which method has not been called with the expectation. Sometimes, having multiple verifications as above can be problematic. For example, if the Save method has not been called, you would never know the Delete method has been called or not. This is because the exception has been thrown when the Save method has not been called and the test execution has been terminated.</p> <p>This would result in multiple tests but they are more readable and maintainable. Of course you can refactor the common code into a factory or helper method.</p> <pre><code>[Fact] public void DeleteNote_RemoveNotes_ReturnsExpectedNotes() { // Arrange var note = new Note { Id = Guid.NewGuid() }; var fakeSubmissionVersion = new SubmissionVersion() { Notes = new List&lt;Note&gt;() { note } }; var repo = new Mock&lt;IRepository&gt;(); repo.Setup(x =&gt; x.GetById&lt;Note&gt;(It.IsAny&lt;Guid&gt;())).Returns(note); // Act NotesHelper.DeleteNote(repo.Object, fakeSubmissionVersion, note.Id.Value); // Assert Assert.AreEqual(0, fakeSubmissionVersion.Notes.Count); } </code></pre> <p>Additional Note: Also providing a descriptive Unit test method names improves the readability of the Unit Tests.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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