I'm having difficulties trying to unit test my ASP.NET Core controllers. I'm building on .NET Core 1.0 and I use xUnit + Moq for testing. Service tests work just fine, with an InMemory EF DbContext and DI all over the place.
Now I'm trying to test my controllers and all of our controllers have constructor injection like this:
public HomeController(
IStringLocalizer<HomeController> localizer,
All the other dependencies get resolved correctly and everything works. But I can't get IStringLocalizer to work properly.
If I try to register a dependency in my Services collection and get an instance through DI, then I get an exception saying IHostingEnvironment couldn't be resolved. And I don't want to use a test server or anything, I'm focusing on unit tests for now.
I tried to mock the IStringLocalizer but that didn't work either, it results in a null value.
I'm kinda new to unit testing and .NET Core testing in general, so It's most probably a much simpler issue and it's just that I don't know how to mock and write unit tests correctly. Any help is much appreciated, thanks in advance.
var mock = new Mock>();
RépondreSupprimerstring key = "Hello my dear friend!";
var localizedString = new LocalizedString(key, key);
mock.Setup(_ => _[key]).Returns(localizedString);
_localizer = mock.Object;
_controller = new HomeController(_localizer);