I’m not 100%, but I would say mocking frameworks cannot mock extensions, this may not be true for Typemock. Currently I’m using Moq which seems to be one of the best free mocking frameworks for unit testing in the .NET community. I’ve been very excited of the arrival of the MVC framework from the ASP.NET community because it brings a level a development which is geared more towards test driven design. One to the challenges with test driven design is the mocking of processes that are not relative to the code logic under test. One such case this pops up is if you’re using the IsAjaxRequest helper method in the ASP.NET MVC. If you’re testing your controller method and you try to mock this helper you’ll get an error “Invalid expectation on a non-overridable member”. I don’t know if this the best advice for all types of extension methods but in this case the behavior of the IsAjaxRequest is pretty simple. So if you’re using Moq you would have something like this:
_mockContext.Request.Setup(m => m["X-Requested-With"]).Returns("XMLHttpRequest");
Here the the IsAjaxRequest is checking a header to verify a ajax request. The _mockContext is a mocked context object. You can find the code for mocking the context objects here. This code uses the Moq framework but you be substituted for another framework if desired.
No comments:
Post a Comment