-
-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
when using xunit test(SUT),there is an exception throwed! #68
Comments
Thanks for reporting, I will try to run the tests again and check if there's an exception |
Thanks for replying, When I run my WebApplication in my Unit Test project. JsonStringLocalizerFactory create a JsonStringLocalizer with wrong Resources Path, the path should be in my WebApplication project,turns out the JsonStringLocalizer used my Unit Test project's path. But my Unit Test project doesn't have a directory named Resource, and I want my WebApplication project in unit test still using my Resources path under my WebApplication project, just like I'm using Microsoft's StringLocalization.I don't want to copy my Resources files from my WebApplication project to my Unit Test project. builder.Services.AddJsonLocalization(options => options.ResourcesPath = "Resources"); In my XUnit test project to create a Test for WebApplication project App = new WebApplicationFactory<Program>().WithWebHostBuilder(builder => {});
Client = App.CreateClient();//Client is an instance of HttpClient and then I used Client.Get("route/to/controller/action") After this, the WebApplication project throws an exception System.IO.DirectoryNotFoundException. fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An unhandled exception has occurred while executing the request.
System.IO.DirectoryNotFoundException: Could not find a part of the path '/PathToSolution/TestProject/bin/Debug/net6.0/Resources'.
at System.IO.Enumeration.FileSystemEnumerator`1.CreateDirectoryHandle(String path, Boolean ignoreNotFound)
at System.IO.Enumeration.FileSystemEnumerator`1.Init()
at System.IO.Enumeration.FileSystemEnumerator`1..ctor(String directory, Boolean isNormalized, EnumerationOptions options)
at System.IO.Enumeration.FileSystemEnumerable`1..ctor(String directory, FindTransform transform, EnumerationOptions options, Boolean isNormalized)
at System.IO.Enumeration.FileSystemEnumerableFactory.UserFiles(String directory, String expression, EnumerationOptions options)
at System.IO.Directory.InternalEnumeratePaths(String path, String searchPattern, SearchTarget searchTarget, EnumerationOptions options)
at System.IO.Directory.EnumerateFiles(String path, String searchPattern)
at My.Extensions.Localization.Json.Internal.JsonResourceManager.TryLoadResourceSet(CultureInfo culture)
at My.Extensions.Localization.Json.Internal.JsonResourceManager.GetResourceSet(CultureInfo culture, Boolean tryParents)
at My.Extensions.Localization.Json.Internal.JsonResourceManager.GetString(String name)
at My.Extensions.Localization.Json.JsonStringLocalizer.GetStringSafely(String name, CultureInfo culture)
at My.Extensions.Localization.Json.JsonStringLocalizer.get_Item(String name)
at Microsoft.Extensions.Localization.StringLocalizer`1.get_Item(String name) |
Please create a PR showing your unit test which thrown the exception, then I can try to figure out why it's happening |
Sorry,I don't get it. What is PR? Pull Request? Demo? Or something others?I gonna Google it.Wait me back.Thks. |
Hi, hishamco. using System.IO;
using System.Reflection;
namespace My.Extensions.Localization.Json.Internal
{
public static class PathHelpers
{
public static string GetApplicationRoot() => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
}
} Never mind! builder.Services.AddJsonLocalization(options =>
{
options.ResourcesPath =
Path.Combine(GlobalServiceProvider.App!.Environment.ContentRootPath, "Resources");
//GlobalServiceProvider.App.Environment is type of IWebHostEnvironment
}); Thanks for reply.Thank you very much. By the way,PR => Pull request. got it!. |
Pull Request, just to reproduce the issue |
No problem at all, the important thing is everything works as expected ;) |
Here is the code in my xunit test project
when I run a unit test,the web application throw exception of DirectoryNotFoundException.the console output is under below.
I noticed that JsonStringLocalizerFactory.Create Method called PathHelpers.GetApplicationRoot(),the PathHelpers.GetApplicationRoot() Method simply call Assembly.GetExecutingAssembly().Location. that's the problem. Assembly.GetExecutingAssembly().Location Gets the current executing assembly which is my test project assembly. it returned a path of my test project build directory. normally, I want JsonStringLocalizer to locate the resources file in the Resource directory under my web project, but a unit test can't do that.the Microsoft's StringLocalization is working well in my unit test project.I also noticed that they are using WebApplicationFactoryContentRootAttribute to resolve the localization resources directory. check this document How the test infrastructure infers the app content root path
.Please solve this issue.If something I've missed or went wrong,Please tell me.Thank you.
The text was updated successfully, but these errors were encountered: