last day (15 days later) » 

1:57 AM
0
Q: ASP.Net Core 2.0 SignInAsync returns exception Value cannot be null, provider

t.j.I have an ASP.Net Core 2.0 web application I am retrofitting with unit tests (using NUnit). The application works fine, and most of the tests thus far work fine. However, testing the authentication/authorization (does a user get logged in and can access [Authorize] filtered actions) is failing ...

 
@Aeseir - updated original post
 
Consider abstracting the sign in call out so as to be able to mock it when testing in isolation.
 
@Nkosi - Certainly worth consideration. I (don't yet) think that portion is necessary to isolate and understand the primary focus of my question though (correct?) That being: why is it breaking, and why is it being reported/reflected inconsistently.
 
@t.j. Take a look at the answer I gave here stackoverflow.com/a/47199298/5233410
 
@Nkosi - Is that necessary though? I have been using the msdn docs as a reference, docs.microsoft.com/en-us/aspnet/core/mvc/controllers/testing
 
1:57 AM
@t.j. note that the docs are not accessing/calling the same members that you are using. You are calling features that the framework would setup for you at run time. During isolated unit tests you will need to set these up yourself. In this case you will need to mock the service provider so that the extension method does not fail.
 
@Nkosi - Ah! That is what I feared. So there is some service that is not being provided that is necessary? Am I crazy in thinking that ascertaining that from the reported errors is not very direct? Worth a bug report?
Actually, I am just calling a method off the controller just as the docs are. But is it missing something that Startup normally would have provided?
 
The HttpContext is missing an IServiceProvider which it uses to resolve IAuthenticationService which is what actually calls SignInAsync
And seeing as you created the context manually it was your responsibility to provide one
If you have to call start up then you are leaving unit testing and crossing over into Integration testing territory
Are you trying to run an Unit test or an integration test?
 
 
2 hours later…
4:25 AM
Intending to start with unit tests on the controller (to ensure that, in a known state, the SUT will yield the correct route/page.) I suppose a good argument could be made that with the pieces involved, it is more of an integration test?
Or perhaps more accurately, it should be understood in terms of: since I am not testing one method alone, but underlying components who themselves have requirements/dependencies... it is more of an integration test at that point. To unit test would be best approached using the mocking (Moq) scenario you proposed?
"creating the context manually", are you referring to my creation/use of a DataContext (ala: Utils.GetInMemContext();, which returns a datacontext with an in-mem sqlite dbcontext)?
Wait, how is it that my Controller only needs/uses a repository object (only param in ctor), and runs (non-unit test)) without issue (when accessing HttpContext), BUT I must create an overloaded ctor that accepts an IServiceProvider so that I can test it (when it runs without it fine in non-test/normal mode)? How is it getting the services now?
 
4:56 AM
@Nkosi - Sorry I had to step away. Hoping you can weigh in on my follow up questions (and didnt know if you would be notified about them while not tagged or in the room.)
 

last day (15 days later) »