ASP.NET MVC NullReferenceException when inheriting from a Base Controller -


I've got a base controller that I get from all my controllers. This work is basically to set caching and error handling and to check mobile browsers as well.

My UI works fine, but my unit test is failing.

  import system. Web.Mvc & lt; HandleError () & gt; _ & Lt; CompressFilter () & gt; _ & Lt; Output Cache (Duration: = 30, VaryByParam: = "id") & gt; _ Public Class BaseController: View the inherited System.Web.Mvc.Controller protected override function (ByVal as the VIEWNAME string, ByVal as masterName string, as the ByVal model object) System.Web.View.Movc.ViewResult is slow As the Nullable (Boolean) form = Request.Browser.IsMobileDevice isMobile then returning to MyBase.View (VIEWNAME, "mobile", model) or else Return MyBase.View (VIEWNAME, "Site", Model) from second to second Ending the end of the function then the class  

Error I will be in my unit exam On the Nullable (Boolean a) = Request.Browser.IsMobileDevice as isMobile not set dim call

object reference to an instance of an object.

Edit:

My test class looks like this

  Importing System. Web MVC Import UrbanNo.Core Import Microsoft.Visual Studio. Test Tools Unit testing & lt; Test class () & gt; Public Class Events Comptroller Testing & lt; TestMethod () & gt; Public sub-index () '' # Dim controller EventsController = New EventsController (as arranged as) '' # Dynamic result viewResult = CTYPE (controller.Index (), ViewResult) '' #Estudently ViewData ViewDataDictionary = result.ViewData As the end of the sub end class  

It is just that when you create a new MVC web application, a rip of the examination stops.

It is believed that is requested. Broser is an extension property, it is one of the logical places for the exception of being thrown. Alternatively, it tries to implement IsMobileDevice which is empty when exiting a unit test environment.

Edit After seeing the test code, it seems that the problem is that the request property on the controller is never set when the page If ASP is executed by .NET, then the frame fills him with a valid object. When the unit is tested, you need to provide a duplicate for the request which is not enough to make the test crash.

There are two ways to do this:

  1. Get the browser from RequestBase to crash Create a class and implement it adequately.
  2. To make a duplicate of a joke over time (like), Runtime will still need to implement the Browser.IsMobile functionality actually working.

My suggestion to use request to use a muke to return another tomb, which abandons the browser property Then you can just return true / incorrect for IsMobileDevice , so that you can separate the controller functionality about how Browser.IsMobileDevice works.

Example code without worrying in C #:

  Fake & lt; Requestbase & gt; RequestMock = new fake & lt; RequestBase & gt; (); Fake & lt; Browser & gt; BrowserMock = new fake & lt; Browser & gt; (); RequestMock.Setup (request => request.Browser) .Returns (browserMock.Object); BrowserMock.Setup (Browser => Browser.IsMobileDevice) .Returns (true); Controller Request = Request Mock.Object;  

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -