Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to i unit test this method?

I need to unit test this method. I'm using moq as my mocking framework, if that helps.

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

cheers :)

EDIT: It was mainly the FormsAuthentication i was wondering. Should I even be testing that? I suppose i would need to mock up an Identity and then check the IsAuthenticated is false?

like image 244
Pure.Krome Avatar asked Dec 29 '25 06:12

Pure.Krome


1 Answers

you could create a wrapper for FormsAuthentication and stub it out

formsAuthentication = mockery.Stub<IFormsAuthentication>();

and do something like this.

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

 /* Asserts to go here */

In terms of testing the FormsAuthentication. Test this somewhere else, separation of concerns and all that.

The test for this action merely needs to check that the SignOut method has been called and if the redirect has happened. Remember you are testing the action, not the methods within the action. You wouldn't test the data store work in a Submit action, you'd mock all that out.

like image 101
John Polling Avatar answered Jan 01 '26 11:01

John Polling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!