所以我试图测试这个方法将返回“ManagerEventsIndex”的情况:
public ActionResult Index()
{
if (this.CurrentUser != null)
{
if (this.CurrentUser.Role.Name == AvailableRoles.GameManager)
{
var model = new GameManagerEventsIndexModel();
model.NumLocationsOfCurrentUser = this._customerLocationDataProvider.GetAllForUserId(CurrentUser.Id)
.Where(x => x.Active == true)
.Count();
return View("ManagerEventsIndex", model);
}
else
{
return View();
}
}
else
return View("PublicIndex");
}
但是这个控制器实现了另一个控制器ApplicationController,如下所示:EventsController:ApplicationController,它是Application控制器中的CurrentUser对象,我需要模拟它来测试它......这是我到目前为止的测试方法。谢谢。
[TestMethod]
public void AssertGameManagerRoleReturnsGameManagerIndex()
{
// Arrange
//eventsController = new EventsController(eventDataProvider.Object, playerEventDataProvider.Object, userDataProvider.Object,
tokenAuthent.Object, dataContext.Object, customerLocationDataProvider.Object);
var user = new Mock<User>();
var role = new Mock<Role>();
role.SetupSet(x => x.Name = "Ted");
user.SetupSet(x => x.Role = role.Object);
//?? var appController = new Mock<ApplicationController>();
//?? appController.SetupSet(x => x.User = user);
// var mController = new Mock<EventsController>();
// Act
var result = (ViewResult) eventsController.Index();
// Assert
Assert.AreEqual("ManagerEventsIndex", result.ViewName);
}