I have the following methods in my Authentication class that is called by my controller. I know the controller can call the SetAuthCookie itself, but I’d prefer the additional abstraction.
public void FormsAuthSignIn(string loginName, bool rememberMe = false)
{
FormsAuthentication.SetAuthCookie(loginName, rememberMe);
}
Just looking at FormsAuthentication in the object browser reveals no members that I can use to see if this has worked? Should I build a test controller using the Authorize attribute and call a method on it inside my test? What should I do here?
I believe that the only thing to be unit tested here is that
SetAuthCookiewas called with the right parameters. You don’t need to testFormsAuthentication‘s logic.This can be achieved by hiding
FormsAuthenticationbehind an interface, and implementing your own stub for it just for tests, that will count calls on methods.Any mocking framework will help you with this task, while Moles / Typemock Isolator and such will allow you to mock
SetAuthCookiedirectly.Beyond that, IMHO, in this case:
Only
Fooshould get tested – all the other methods are an implementation detail.Same as in this case – There should be a test for the caller of
FormsAuthSignIn, that asserts thatFormsAuthSignInis called in the right cases.