c# - Use Moq to verify if list within object is changed properly -


i'm trying add moq tests in mstest test parts of code.

the code want test not working piece of code should filter data retreived service , pass through. code set through mvp pattern , have following components. (i'm testing presenter)

  • service -> service retrieving list of objects , putting in model (i'm using mock (moq) return values)

  • model -> entity object general properties , list of documents

  • view -> interface usercontrol implementing talk presenter. view mocked moq.

  • presenter -> object retrieve model service , assign model property of view.

in first scenario working retrieve model service , presenter passes property of view.

//setup accountspayableservice mock _mockeddocumentservice = new mock<idocumentservice>(); documentmodel<invoicedocumentrow> model = new documentmodel<invoicedocumentrow>(); list<invoicedocumentrow> invoices = new list<invoicedocumentrow>(); invoicedocumentrow row = new invoicedocumentrow(); row.billingmonth = datetime.now; invoices.add(row); model.documents = invoices; _mockeddocumentservice.setup(service => service.getinvoicedocumentlist(it.isany<datetime>(), it.isany<datetime>(), _user)).returns(model);  //setup view mock _mockedview = new mock<iinvoicesview>();  //setup presenter tested _presenter = new foopresenter(_mockeddocumentservice.object); _presenter.setview(_mockedview.object);  //act  //these events make presenter call service , assign view property _mockedview.raise(view => view.init += null, new eventargs()); _mockedview.raise(view => view.firstload += null, new eventargs());  //assert _mockeddocumentservice.verify(aps => aps.getinvoicedocumentlist(from, changedto, _user), times.once()); _mockedview.verifyset(view => view.documentlist = model); 

this test runs , working perfectly.

however have case presenter should filter of results got service , assign subset view. reason can't work.

in essential same test code except different method on presenter used retrieves data service, filters , passes view.

when assert on view property did before:

_mockedview.verifyset(view => view.documentlist.documents = filteredmodel.documents); 

i'm getting error:

system.argumentexception: expression not property setter invocation. 

what doing wrong?

this not working cause filteredmodel.documentos in context. view doesn't receive this, receive list came filtering method.

changing little structure i'll suggest create extensions methods , tests them. can simple put list.filterbyname("billy");

so create like:

public static ienumerable<objectfromvdcruijsen> filteredbynome(this ienumerable<objectfromvdcruijsen> enumerable, string name){     if (!string.isnullorempty(name)){             enumerable = enumerable.where(s => s.name.toupperinvariant().contains(name.toupperinvariant()));     }     return enumerable; } 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -