Skip to content

Instantly share code, notes, and snippets.

@hoetz
Created August 9, 2014 16:32
Show Gist options
  • Save hoetz/2faee62f6c1f700158ac to your computer and use it in GitHub Desktop.
Save hoetz/2faee62f6c1f700158ac to your computer and use it in GitHub Desktop.
xunit + mvc 6
[Fact]
public void Index_OneGet_ReturnsViewModel()
{
var sut= new HomeController();
var httpContext = new Mock<HttpContext>().Object;
var actionContext=new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
sut.ActionContext=actionContext;
ViewResult result= sut.Index() as ViewResult;
Assert.True(result.ViewData.Model is StartPageViewModel);
}
using System;
using Microsoft.AspNet.Mvc;
using Web.Model;
namespace Web.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View("Index",new StartPageViewModel());
}
}
}
error on xunit run:
HomeTests.Index_OneGet_ReturnsViewModel [FAIL]
System.NullReferenceException : Object reference not set to an instance of an object
Stack Trace:
at Microsoft.AspNet.Mvc.Controller.View (System.String viewName, System.Object model) [0x00000] in <filename unknown>:0
at Web.Controllers.HomeController.Index () [0x00000] in <filename unknown>:0
at HomeTests.Index_OneGet_ReturnsViewModel () [0x00000] in <filename unknown>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
Finished: test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment