riskfirst / riskfirst.hateoas

Powerful HATEOAS functionality for .NET web api
MIT License
78 stars 25 forks source link

DefaultRouteMap doesn't work when integration testing #7

Closed adamrodger closed 7 years ago

adamrodger commented 7 years ago

In DefaultRouteMap, the routes are discovered using Assembly.GetExecutingAssembly() and then searching for types which extend Controller.

However, when you are performing an integration test using Microsoft.AspNetCore.TestHost (as recommended by Microsoft here) then the 'executing' assembly is actually the test assembly, not the service assembly. This means the routes in the service assembly are never discovered and so custom link policies are never applied.

jamiecoh commented 7 years ago

I was never particularly happy with this way of mapping the Api. But as I never had a specific issue with it, I never got round to improving that. Using ApiExplorer may be one option, but it will take some thought.

If you have any ideas, please feel free to suggest them, either here or in a pull request.

adamrodger commented 7 years ago

I have a similar problem in my application where a helper library needs to reflect over types in the real service library to register them, and I do something like (a bit pseudo-codey):

var thisAssembly = typeof(MyClass).GetTypeInfo().Assembly.GetName().Name;
var IEnumerable<CompilationLibrary> libraries =
    DependencyContext.Default
                     .CompileLibraries                      
                     .Where(l => l.Dependencies.Any(d => d.Name.Equals(thisAssembly)));

var names = libraries.Select(l => l.Name).Distinct();
var assemblies = names.Select(a => Assembly.Load(new AssemblyName(a)));

Then assemblies is the collection of assemblies which directly reference your assembly, so you can loop over them and check their types.

jamiecoh commented 7 years ago

That seems sensible. I'll not get a chance to test this out until the weekend/early next week. Feel free to submit a pull if you need it quicker than that.

jamiecoh commented 7 years ago

I have made this change and added an integration test project to the BasicSample solution. Thanks again for your feedback.