I have a class that handles password changes and expiration checks for exchange mailboxes.
I check for LastPasswordSet on a UserPrincipal.
Now what about TDD?
I want to check if my class handles the password checks correctly by writing some tests. But I cant get my head around how I could mock the UserPrincipal.FindByIdentity(principalContext, [some username]).
I’m about to write a method that return true/false if the password has been changed the last 90 days. So I would like to mock the UserPrincipal so I can set the LastPasswordSet return value in my tests just to check the logic I’m about to write for the “password needs change notification”.
I’ll answer that with the pithy phrase
“Don’t mock types you don’t own”
couldn’t find an authoritative blogpost to back that up. Sample link. Although it seems to be attributed to Joe Walnes.
UserPrincipal if I remember is a .Net framework class related to authentication. Mocking types that are out of your control (can change) can lead to fragile tests.
Instead discover what your design wants from UserPrincipal