我有一個MVC 3應用程序,我試圖單元測試。我從this page讀取了示例,演示瞭如何模擬控制器。這裏是我的測試代碼:控制器的單元測試錯誤結束
[TestMethod]
public void TestAddStudy() {
var controller = new AdminController();
var httpContext = FakeHttpContext();
var context = new ControllerContext(new RequestContext(httpContext, new RouteData()), controller);
controller.ControllerContext = context;
var request = Mock.Get(controller.Request);
request.Setup(r => r.Form).Returns(delegate() {
var nv = new FormCollection();
nv.Add("Name", "Test Study");
nv.Add("IsDefault", "selected");
return nv;
});
var result = controller.CreateStudy(request.Object.Form as FormCollection) as ActionResult;
}
public static HttpContextBase FakeHttpContext() {
var httpContext = new Mock<HttpContextBase>();
var request = new Mock<HttpRequestBase>();
var response = new Mock<HttpResponseBase>();
var session = new Mock<HttpSessionStateBase>();
var server = new Mock<HttpServerUtilityBase>();
httpContext.Setup(ctx => ctx.Request).Returns(request.Object);
httpContext.SetupGet(ctx => ctx.Request.RequestType).Returns("POST");
httpContext.Setup(ctx => ctx.Response).Returns(response.Object);
httpContext.Setup(ctx => ctx.Session).Returns(session.Object);
httpContext.Setup(ctx => ctx.Server).Returns(server.Object);
httpContext.SetupGet(ctx => ctx.User.Identity.Name).Returns("testClient");
return httpContext.Object;
}
這裏是控制器內部的代碼:
[HttpPost]
public ActionResult CreateStudy(FormCollection form) {
Study newStudy = new Study();
TryUpdateModel(newStudy);
newStudy.CreatedBy = CurrentUser;
newStudy.CreatedOn = DateTime.Now;
studyRepository.Edit(newStudy, CurrentUser);
return RedirectToAction("EditStudy", new { id = 1 });
}
它無法與不設置到對象錯誤的實例的對象引用的TryUpdateModel聲明每次。我在做什麼錯了......或者更重要的是,我怎樣才能讓這個簡單的方法單元測試?
堆棧跟蹤: 在Microsoft.Web.Infrastructure.DynamicValidationHelper.ValidationUtility.CollectionReplacer.GetUnvalidatedCollections(HttpContext的上下文) 在Microsoft.Web.Infrastructure.DynamicValidationHelper.ValidationUtility.GetUnvalidatedCollections(HttpContext的背景下,函數功能 queryStringGetter) 在System.Web.Helpers.Validation.Unvalidated(HttpRequest請求) 在System.Web.Mvc.FormValueProviderFactory。 <。> b_ 0(ControllerContext cc) at System.Web.Mvc.FormValueProviderFactory.GetValueProvider(ControllerContext controllerContext) at System.Web.Mvc.ValueProviderFactoryCollection。 <>Ç _DisplayClassc.b__7(ValueProviderFactory工廠) 在System.Linq.Enumerable.WhereSelectEnumerableIterator 2.MoveNext() at System.Linq.Enumerable.WhereSelectEnumerableIterator
2.MoveNext() 在System.Collections.Generic.List 1..ctor(IEnumerable
1集合) 在System.Linq.Enumerable.ToList [TSource ]](IEnumerable`1源) 在System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(controllerContext controllerContext) 在System.Web.Mvc.ControllerBase.get_ValueProvider() 在System.Web.Mvc.Controller.TryUpdateModel [TModel的( TModel model) at C:\ Visual Studio Projects \ Controllers \ AdminController.cs中的Controllers.AdminController.CreateStudy(FormCollection窗體):第254行 at Tests.UnitTest1.TestAddStudy()in C :\ Visual Studio Projects \ Tests \ UnitTest1.cs:line 32
請包括堆棧跟蹤。 – jrummell
我只是想建議你使用MVC Contrib的TestHelper:http://mvccontrib.codeplex.com/wikipage?title=TestHelper它將成爲編寫測試的easyer。 – VinnyG