How to do unit test this method?

I need unit test this method. I use moq as my fake structure if that helps.

[AcceptVerbs(HttpVerbs.Get)]
public RedirectToRouteResult LogOff()
{
    FormsAuthentication.SignOut();
    return RedirectToAction("Index", "Post");
}

cheers :)

EDIT: It was mainly the FormsAuthentication that I was interested in. Should I even test this? I suppose I would have to come up with an identifier and then check that IsAuthenticated is false?

+3
source share
2 answers

So you do it with Moq. Assumes you created IFormsAuthenticationas a wrapper:

[Test]
public void LogOffSignsUserOut()
{
   var controller = new MyController();
   var authMock = new Mock<IFormsAuthentication>();
   controller.Authentication = authMock.Object;   //inject your mock

   controller.LogOff()

   authMock.Verify(x=>x.SignOff(), Times.Once());
}
+2
source

you can create a wrapper for FormsAuthentication and close it

formsAuthentication = mockery.Stub<IFormsAuthentication>();

and do something like this.

With.Mocks(mockery)
    .Expecting(() => Expect.Call(() => formsAuthentication.SignOut()))
    .Verify(() => controller.LogOff());

 /* Asserts to go here */

FormsAuthentication. - , .

, SignOut , . , , . Submit, .

+4

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


All Articles