In the previous article on AssertWasCalled
I presented the Rhino Mocks method for checking whether a method was called during test execution.
Sometimes, though, you'd like to check the exact opposite - that a method was not called.
Let's consider a method that's responsible for checking if a document is currently opened in the application.
Then, when the user wants to open the same document again, instead of opening the document, the existing document window is activated.
But if that window is already active, a message is displayed instead, notifying the user that the document is already opened.
Next, let's say this functionality is provided via two interfaces: IDocumentService and IMessageBoxService.
And that we already created the necessary stubs for these: documentService and messageBoxService.
Our tests then can go something like this:
<TestMethod()>
Public Sub TestOpenDocumentAlreadyOpenedNotActive()
' Arrange: Set the environment up so that the document "A.txt" is already opened, but not active:
. . .
' Act:
OpenDocument("A.txt")
' Assert:
documentService.AssertWasCalled(Sub(its) its.ActivateDocument("A.txt"))
messageBoxService.AssertWasNotCalled(Sub(its) its.Warning("Document 'A.txt' is already opened."))
End Sub
<TestMethod()>
Public Sub TestOpenDocumentAlreadyOpenedAndActive()
' Arrange: Set the environment up so that the document "A.txt" is already opened, and active:
. . .
' Act:
OpenDocument("A.txt")
' Assert:
documentService.AssertWasNotCalled(Sub(its) its.ActivateDocument("A.txt"))
messageBoxService.AssertWasCalled(Sub(its) its.Warning("Document 'A.txt' is already opened."))
End Sub
Quite simple, actually; though with both methods, and especially with AssertWasNotCalled,
you often want to check that the method was not called at all, regardless what parameters were passed to the method
(note that in the examples above, we're checking that messageBoxService.Warning was/wasn't called with that specific text;
it could've been called with text that's slightly different...).
But that's a slightly wider topic and will be discussed in a separate article.
HTH
Top
|