Note that there are some explanatory texts on larger screens.

plurals
  1. POUse Moq to verify if list within object is changed properly
    primarykey
    data
    text
    <p>I'm trying to add Moq to my tests in MSTest to test parts of my code.</p> <p>The code i want to test which is not working is a piece of code that should filter data retreived by a service and pass this through. My code is set up through the MVP Pattern and i have the following components. (i'm testing my presenter)</p> <ul> <li><p>Service -> this service is retrieving a list of objects and putting this in a model (I'm using a Mock (Moq) to return values)</p></li> <li><p>Model -> Entity object with some general properties and a list of documents</p></li> <li><p>View -> The interface my usercontrol is implementing to talk to the presenter. this view is also mocked with moq.</p></li> <li><p>Presenter -> object to retrieve the model from the service and assign this model to a property of the view.</p></li> </ul> <p>in my first scenario that is working i just retrieve a model from the service and the presenter passes this to a property of the view.</p> <pre><code>//Setup AccountsPayableService Mock _mockedDocumentService = new Mock&lt;IDocumentService&gt;(); DocumentModel&lt;InvoiceDocumentRow&gt; model = new DocumentModel&lt;InvoiceDocumentRow&gt;(); List&lt;InvoiceDocumentRow&gt; invoices = new List&lt;InvoiceDocumentRow&gt;(); InvoiceDocumentRow row = new InvoiceDocumentRow(); row.BillingMonth = DateTime.Now; invoices.Add(row); model.Documents = invoices; _mockedDocumentService.Setup(service =&gt; service.GetInvoiceDocumentList(It.IsAny&lt;DateTime&gt;(), It.IsAny&lt;DateTime&gt;(), _user)).Returns(model); //Setup View Mock _mockedView = new Mock&lt;IInvoicesView&gt;(); //Setup Presenter to be tested _presenter = new FooPresenter(_mockedDocumentService.Object); _presenter.SetView(_mockedView.Object); //Act //These events will make the presenter do the call to the service and assign this to the view property _mockedView.Raise(view =&gt; view.Init += null, new EventArgs()); _mockedView.Raise(view =&gt; view.FirstLoad += null, new EventArgs()); //Assert _mockedDocumentService.Verify(aps =&gt; aps.GetInvoiceDocumentList(from, changedTo, _user), Times.Once()); _mockedView.VerifySet(view =&gt; view.DocumentList = model); </code></pre> <p>This test runs and is working perfectly.</p> <p>however i also have a case where the presenter should filter some of the results it got back from the service and assign a subset to the view. For some reason i can't get this to work.</p> <p>in essential this is exactly the same test code except a different method on the presenter is used that retrieves data from the service, filters it and then passes it back to the view.</p> <p>when i do an assert on the view property like i did before: </p> <pre><code>_mockedView.VerifySet(view =&gt; view.DocumentList.Documents = filteredModel.Documents); </code></pre> <p>i'm getting an error:</p> <pre><code>System.ArgumentException: Expression is not a property setter invocation. </code></pre> <p>What am i doing wrong?</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.
 

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