JonPSmith / EfCore.TestSupport

Tools for helping in unit testing applications that use Entity Framework Core
https://www.thereformedprogrammer.net/new-features-for-unit-testing-your-entity-framework-core-5-code/
Other
352 stars 53 forks source link

GetCallingAssemblyTopLevelDir method via Nuget gives wrong results #4

Closed mburnham closed 6 years ago

mburnham commented 6 years ago

I've been trying to use the CreateUniqueClassOptions method to set up a SQL Server xUnit test via your Nuget package. I cannot get it to work properly. I believe I have discovered that the cause is the use of use of var pathToManipulate = (callingAssembly ?? Assembly.GetCallingAssembly()).Location;

in the GetCallingAssemblyTopLevelDir method. It appears that the result of this is not as expected, since the assembly being referenced is the Nuget package, which does not have the "bin" directory that you reference. As such, it causes a problem with your indexOfPart var returning zero.

Also, I think your logic here might be wonky: var indexOfPart = pathToManipulate.IndexOf(binDir, StringComparison.OrdinalIgnoreCase) + 1; if (indexOfPart < 0) throw new Exception($"Did not find '{binDir}' in the ApplicationBasePath");

If the value for "bin" is not found, then your IndexOf will be -1. You then add 1 to that, which would result in zero. You then check to see if the result is LESS than zero. You need to check if it equals zero. Right now, it blows up in the Substring function with a length of -1.

JonPSmith commented 6 years ago

Hi @mburnham,

Sorry about that. I had a bug inside my code around referring to the correct assembly, plus another bug that should have provided a more friendly error message when that happened. I have fixed both and there is a new version 1.1.5 available on NuGet.

mburnham commented 6 years ago

Thanks, Jon. That fixed it for me.

JonPSmith commented 6 years ago

Good. Thank you for reporting the bug - I should have caught that earlier.