English 中文(简体)
ASP.NET MVC - test the controller returning different views depending on action method logic
原标题:

my controller can return different views depending on action method logic. Action method Create asks service to do some validation and persistence. If validation fails, action method returns same Create view. If validation and save runs OK, action method returns Index view (RedirectToAction). I know that getting view name is possible only when you explicitly provide the view name like follows :

return View("Create", data);

I don t wan t to hardcode the view name only because of tests, but can t figure out how to find which view was returned. Is there a clean and elegant way to test which view was returned depending on action method logic?

BTW, here is my test code..

[TestMethod]
        public void Create_Post_Should_Return_Create_View_When_Saving_Invalid_Court() {
            var courtServiceMock = new Mock<ICourtService>();
            var userServiceMock = new Mock<IUserService>();
            courtServiceMock.Setup(x => x.Add(It.IsAny<CourtInfo>())).Returns((int?)null);
            userServiceMock.Setup(x => x.Get(It.IsAny<string>(), It.IsAny<UserLoadOptions>())).Returns(new UserInfo { Id = 1 });
            var controller = new CourtsController(courtServiceMock.Object, userServiceMock.Object);
            var controllerContextMock = new Mock<ControllerContext>();
            controllerContextMock.SetupGet(x => x.HttpContext.User.Identity.Name).Returns("admin");
            controller.ControllerContext = controllerContextMock.Object;
            var view = controller.Create(new CourtInfo()) as ViewResult;
            Assert.IsTrue(view.ViewName == "Create"); //this line is not working
        }
最佳回答

You should test the type of the result, rather than the name of the view. If you redirect, the result won t be a ViewResult anyway.

If you return RedirectToAction in case of an error, then you would do tests like:

// setup for success
var result = controller.Create(new CourtInfo());

Assert.IsInstanceOfType(result, typeof(ViewResult));

Error test:

// setup for error
var result = controller.Create(new CourtInfo());

Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
问题回答

暂无回答




相关问题
run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

How to unit-test an enterprise symfony project?

I´m working on a huge project at my work. We have about 200 database tables, accordingly a huge number of Models, Actions and so on. How should I begin to write tests for this? My biggest problem ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Unit testing. File structure

I have a C++ legacy codebase with 10-15 applications, all sharing several components. While setting up unittests for both shared components and for applications themselves, I was wondering if there ...

Unit Testing .NET 3.5 projects using MStest in VS2010

There s a bug/feature in Visual Studio 2010 where you can t create a unit test project with the 2.0 CLR. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483891&wa=...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签