Using MSTest and Fakes as my mocking framework, I was writing unit tests against an MVC controller and I needed to mock Controller.UpdateModel<TModel>(TModel). How do you do that? How do you fake out methods with generics? In my example, in my controller action, I’m pulling a Series object from my repository and I want UpdateModel() to update the Series object:
1 2 |
var fromRepo = _bookRepo.GetSeries(theSeries.Id); UpdateModel<MyApp.Domain.EFEntities.Series>(fromRepo); |
In my Unit Test, I shim UpdateModel<Series>() like this:
1 2 3 4 5 6 7 |
// create updateModelShim FakesDelegates.Action<Controller, Series> updateModelShim = (self, series) => { // do whatever I need to in my shim for UpdateModel here }; // set updateModelShim to the controller ShimController.AllInstances.UpdateModelOf1M0(updateModelShim ); |