Tag: mvccontrib testhelper

使用Automapper映射ViewModel之后我应该测试什么?

我试图测试控制器的Index操作。 该操作使用AutoMapper将域Customer对象映射到视图模型TestCustomerForm 。 虽然这有效,但我担心测试我从Index操作中获得的结果的最佳方法。 控制器的索引操作如下所示: public ActionResult Index() { TestCustomerForm cust = Mapper.Map(_repository.GetCustomerByLogin(CurrentUserLoginName)); return View(cust); } 它的TestMethod看起来像这样: [TestMethod] public void IndexShouldReturnCustomerWithMachines() { // arrange var customer = SetupCustomerForRepository(); // gets a boiler plate customer var testController = CreateTestController(); // act ViewResult result = testController.Index() as ViewResult; // assert Assert.AreEqual(customer.MachineList.Count(), (result.ViewData.Model as TestCustomerForm).MachineList.Count()); } 在CreateTestController方法中,我使用Rhino.Mocks来模拟客户存储库并将其设置为从SetupCustomerForRepository返回客户。 通过这种方式,我知道当Index操作调用_repository.GetCustomerByLogin(CurrentUserLoginName)时,存储库将返回目标客户。 […]