My (legacy) MVC application has the following hierarchy of controller classes:
public class CMSController : BaseController
{
public virtual void SetModelValues(ModelBase model)
{
model.CurrentPageURL = Request.Url.ToString();
// set more model data...
}
}
public class PageController : CMSController
{
public ActionResult Index()
{
MyModel model = new MyModel();
base.SetModelValues(model);
ViewBag.Title = "My Title";
return View(model);
}
}
I am writing unit tests for the PageController.Index method.
I am trying to Fake the call to base.SetModelValues, so that it is not invoked, but can not get this to work.
var fake = Isolate.Fake.Instance<CMSController>();
Isolate.Swap.AllInstances<CMSController>().With(fake );
Isolate.WhenCalled(() => fake.SetModelValues(null)).WillThrow(new Exception("my exception"));
// Creating an instance of PageController
PageController _controller = new PageController();
// Call the index method
var result = _controller.Index() as ViewResult;
Assert.IsNotNull(result);
When I run my test however, the actual base.someVirtualMethod still gets invoked.
Can I declare a Fake (or Mock?) in my test which will prevent the real base.someVirtualMethod from running?