6 messaggi dal 08 giugno 2015
Hello Marco,

VERY interesting and helpful article. It is very difficult to find online resources about ASP.NET Identity and IoC/Ninject.

One question please about Phase 4, can you make ApplicationSignInManager and ApplicationUserManager private variables?

Also, can you mock ApplicationSignInManager and ApplicationUserManager using Moq for Unit Testing? If yes, what is your approach? Do you use something like this:

var store = new Mock<IUserStore<ApplicationUser>>(MockBehavior.Strict);

and then create an instance of the Controller like this var controller = new AccountController(new UserManager<ApplicationUser>(store.Object));

You Used the term ' constructor injection' is it the same as 'dependency injection'?
6 messaggi dal 08 giugno 2015
Hello Marco,

VERY interesting and helpful article. It is very difficult to find online resources about ASP.NET Identity and IoC/Ninject.

One question please about Phase 4, can you make ApplicationSignInManager and ApplicationUserManager private variables?

Also, can you mock ApplicationSignInManager and ApplicationUserManager using Moq for Unit Testing? If yes, what is your approach? Do you use something like this:

var store = new Mock<IUserStore<ApplicationUser>>(MockBehavior.Strict);

and then create an instance of the Controller like this var controller = new AccountController(new UserManager<ApplicationUser>(store.Object));

You Used the term ' constructor injection' is it the same as 'dependency injection'?

PS: You can write in Italian as I understand it perfectly. I just am ot very good in writing it.
Hi, thank you

1) yup, you definitely can make those properties private.
2) I haven't personally tested it, but you should be able to configure your IoC container using the interfaces instead of the concrete types, and it that case pluggin in Moq becomes a breeze.
3) Constructor Injection is a way to inject dependencies in an object, providing parameters in the constructor and letting your IoC container do the magic.

Does it make sense?

Cheers,
m.
6 messaggi dal 08 giugno 2015
Thank you for the very prompt reply!

Point 1 and 3 are clear but point 2 is killing me from the inside :)

To give you an idea I wish to implement some Unit Test like the answer provided in the link http://forums.asp.net/t/1993211.aspx?Mocking+the+IUserStore+UserManager+and+MOQ and yet keep ApplicationUserManager as my UserManager class. Any Ideas or suggestions how can I mock ApplicationUserManger?

Do I need to implement IApplicationUserManger? Or do I make the Controller use UserManager<ApplicationUser> in the constructor instead of ApplicationUserManager? If yes, can you provide examples? (Still a bit green in IoC and Mocking)
6 messaggi dal 08 giugno 2015
I have been think and researching and I think I found a solution.

1. Keep code as shown in the post.
2. Create a Unit Test
3. Mock IUserStore as shown in this link: http://forums.asp.net/t/1993211.aspx?Mocking+the+IUserStore+UserManager+and+MOQ
4. Create a new instance of ApplicationUserManger in the test as shown here: https://social.msdn.microsoft.com/Forums/en-US/b74e76eb-6b52-4d55-a194-9cde06992410/how-to-mock-up-applicationusermanager-in-a-unit-test?forum=vsunittest
5. Pass the new instance to the controller.

What do you think?
6 messaggi dal 08 giugno 2015
Continuing with my last post ... Since ApplicationUserManager also requires IdentityFactoryOptions can I just use

new IdentityFactoryOptions<ApplicationUserManager>()
{
DataProtectionProvider = Startup.DataProtectionProvider
});

in my test project?
Hi, sorry for the delay, very busy yesterday.

With ApplicationUserManager unfortunately things get more tricky, since it doesn't implement a specific interface. The same happens with ApplicationSignInManager.

Anyway, we're lucky enough that those classes belong to the Web Project, so nothing prevents you from creating your IApplicationUserManager interface, implement it and put all the members you use in your controllers.

So, your controller will now accept a IApplicationUserManager instance in the constructor, and this can be easily mocked.

Makes sense?
m.
6 messaggi dal 08 giugno 2015
I will try out and leave a comment once I manage to implement a solution. Who knows maybe it help others too!

Thanks a lot for your kind assistance for now! :)

Torna al forum | Feed RSS

ASPItalia.com non è responsabile per il contenuto dei messaggi presenti su questo servizio, non avendo nessun controllo sui messaggi postati nei propri forum, che rappresentano l'espressione del pensiero degli autori.