Skip to content
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

Closed
codelovercc opened this issue Jul 9, 2022 · 7 comments
Closed

when using xunit test(SUT),there is an exception throwed! #68

codelovercc opened this issue Jul 9, 2022 · 7 comments

Comments

@codelovercc
Copy link

Here is the code in my xunit test project

App = new WebApplicationFactory<TProgram>().WithWebHostBuilder(builder => {});
Client = App.CreateClient();

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.

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)
@hishamco
Copy link
Owner

hishamco commented Jul 9, 2022

Thanks for reporting, I will try to run the tests again and check if there's an exception

@codelovercc
Copy link
Author

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.
Is that clear enough? Sorry, English is not my forte.
In my WebApplication project's Main method:

        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)

@hishamco
Copy link
Owner

hishamco commented Jul 9, 2022

Please create a PR showing your unit test which thrown the exception, then I can try to figure out why it's happening

@codelovercc
Copy link
Author

Sorry,I don't get it. What is PR? Pull Request? Demo? Or something others?I gonna Google it.Wait me back.Thks.

@codelovercc
Copy link
Author

Hi, hishamco. IWebHostEnvironment.Environment.ContentRootPath this is what i want.
Is there a way to use IWebHostEnvironment.Environment.ContentRootPath in class PathHelpers.GetApplicationRoot()

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!
After all this, I've made a mistake. I have a typo about my Resource directory name. I typed Recourse as my Resource directory name. so this exception was thrown.Sorry to bother you.
Now I just correct the resource directory name and modify my code in the Program.cs

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!.
Please close this issue.

@hishamco
Copy link
Owner

Sorry,I don't get it. What is PR?

Pull Request, just to reproduce the issue

@hishamco
Copy link
Owner

Never mind!
After all this, I've made a mistake. I have a typo about my Resource directory name. I typed Recourse as my Resource directory name. so this exception was thrown.Sorry to bother you.

No problem at all, the important thing is everything works as expected ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants