Unit testing Asp.net MVC controller action

I have a controller action that checks

this.User.Identity.IsAuthenticated

What do you suggest how to solve unit test with such an action?

+3
source share
1 answer

I suggest making fun of the IsAuthenticated property. There are several more posts about this, you can search for them.

Here is an example of query bullying using Moq:

var mockRequest = new Mock<HttpRequestBase>();
mockRequest.Setup(x => x.IsAuthenticated).Returns(true); 

var mockContext = new Mock<ControllerContext>();
mockContext.Setup(x => x.Request).Returns(mockRequest.Object);

var myController = new MyController();
myController.ControllerContext = new ControllerContext(mockContext.Object, new RouteData(), myController);

I would really like to take a look at the ubiquitous Scott Hanselman code "MvcMockHelpers" that I use:

http://www.hanselman.com/blog/ASPNETMVCSessionAtMix08TDDAndMvcMockHelpers.aspx

+5
source

Source: https://habr.com/ru/post/1712619/


All Articles