Note that there are some explanatory texts on larger screens.

plurals
  1. POMVVM unit tests - message bus only works once
    primarykey
    data
    text
    <p>I am trying to write some unit tests against a view model using SimpleMvvmToolkit, and several of these tests require a message be sent to the message bus to fire events inside the view model. The problem is it appears that I can only 'use' the message bus once; the first test that sends a message passes, but the other two that send a message fail. But if I run each test individually, all three pass, and if I change the order, the first test, no matter which one it is, passes.</p> <p>Here is a sample of one of the tests.</p> <pre><code>[TestMethod] public void DeleteRequest() { // mock the driver transaction service var driverTransactionService = new Mock&lt;Services.IDriverTransactionService&gt;(); var viewModel = new ValidationRequestViewModel( driverTransactionService.Object); driverTransactionService.Setup(dts =&gt; dts.DeleteValidationRequest(It.IsAny&lt;Action&lt;int&gt;&gt;(), It.IsAny&lt;Action&lt;Exception&gt;&gt;(), It.IsAny&lt;int&gt;())) .Callback((Action&lt;int&gt; action, Action&lt;Exception&gt; ex, int id) =&gt; action.Invoke(requestId)); // make a validation request and add to the collection var validationRequest = new ValidationRequest... var collection = viewModel.ValidationRequestView.SourceCollection as ObservableCollection&lt;ValidationRequest&gt;; collection.Add(validationRequest); // delete the validation request; send the confirmation message as if // the user clicked yes viewModel.DeleteValidationRequest(validationRequest); MessageBus.Default.Notify( NotificationMessages.DeleteValidationRequestConfirmation, this, new NotificationEventArgs&lt;String&gt;(null, null)); // verify the service call was made driverTransactionService.Verify(dts =&gt; dts.DeleteValidationRequest(It.IsAny&lt;Action&lt;int&gt;&gt;(), It.IsAny&lt;Action&lt;Exception&gt;&gt;(), requestId)); } </code></pre> <p>So I am creating a mock of a service (which is a wrapper around a WCF service) and verifying that DeleteValidatonRequest on the service is being called. In this case, the message bus is needed because a confirmation dialog would normally pop up and send the DeleteValidationRequestConfirmation message if the user clicks OK.</p> <p>The test fails on the last line (Verify) and when I debug, the handler for the message in the view model never executes so it looks like the message is never actually sent. Again, this only fails if the test is <strong>not</strong> the first test to send a message to the bus. If it executes alone or is the first, the test passes.</p> <p>Any ideas? All of the tests are independent and do not share any properties or variables of the test class so I don't see how they could be stepping on each other.</p> <p>EDIT: I used reflection to call the protected methods on the view model that the messages would normally fire; this gets me the code coverage I want, it just doesn't verify that the view model is responding to messages correctly.</p> <p>EDIT 2: Crap, the same thing is happening with PropertyChanged events on the view model. The event handler only fires if the test is run independently or is the first.</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. This table or related slice is empty.
    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