asp.net core testing controller with IStringLocalizer
Setup the mock to return your expected result.
var mock = new Mock<IStringLocalizer<HomeController>>();
string key = "Hello my dear friend!";
var localizedString = new LocalizedString(key, key);
mock.Setup(_ => _[key]).Returns(localizedString);
_localizer = mock.Object;
_controller = new HomeController(_localizer);
If you need strings from the actual localized resources in your tests you can add the Microsoft.AspNetCore.All Nuget package to your test project and then use the following code:
var options = Options.Create(new LocalizationOptions {ResourcesPath = "Resources"});
var factory = new ResourceManagerStringLocalizerFactory(options, NullLoggerFactory.Instance);
var localizer = new StringLocalizer<HomeController>(factory);
The ResourcesPath
should be the relative path of where HomeController.en.resx is found from the project root.