Identity icon indicating copy to clipboard operation
Identity copied to clipboard

Very difficult to unit test with SignInManager

Open craigahanson opened this issue 9 years ago • 6 comments

I am really struggling to add unit tests over a service I have developed which has a dependency on the SignInManager. Is there a reason there is no interface for the sign in manager?

craigahanson avatar Jul 27 '17 22:07 craigahanson

I have similar concerns about the dependencies on classes rather than interfaces. they should get removed. Ie. the constructor of SignInManager has a hard dependency on UserManager<TUser>, rather than on IUserManager<TUser> (which doesn't exist either).

My workaround so far was to subclass everything and put proper interfaces on them.

MovGP0 avatar Jul 29 '17 05:07 MovGP0

The main reason was we wanted the ability to add new API to the managers without causing breaking changes. You can still mock the managers, see: https://github.com/aspnet/Identity/blob/dev/test/Microsoft.AspNetCore.Identity.Test/SignInManagerTest.cs#L197

What is it you are trying to do that an interface would let you do that you can't do today in your tests? Effectively the manager types are the interface, you can register your own manager type with DI which will take over the implementation.

HaoK avatar Jul 29 '17 16:07 HaoK

If the UserManager and SignInManager had interfaces then it allows you to make fake implementations and therefore is easy to unit test and DI.

I tried to workaround this issue by creating subclasses and applying interfaces to them but I seem to run into issues in the SignInManager, as it must be supplied with a concrete UserManager class.

I am wondering whether or not to create a pull request.. do you think it would be worth it or is it not something that is wanted anyway?

craigahanson avatar Aug 11 '17 12:08 craigahanson

We still unit test them and fake them in the tests whether or not they are interfaces or classes, you can still use something like Moq to fake and inject them, see:

https://github.com/aspnet/Identity/blob/dev/test/Shared/MockHelpers.cs#L19

HaoK avatar Aug 11 '17 18:08 HaoK

all of these links are broken ...grrrrr!

ppi-rloving avatar Aug 03 '18 15:08 ppi-rloving

For anyone searching, here is the current link: https://github.com/aspnet/Identity/blob/master/test/Shared/MockHelpers.cs

tiagodenoronha avatar Oct 22 '18 15:10 tiagodenoronha