TLDR
Custom AssemblyResolve in a C++/CLR-project, which has to be set up (SetUpAssemblyResolve()) before running the tests.
It simply tries to load the Assembly from "./"
using namespace System;
using namespace ::System::Reflection;
using namespace System::IO;
Assembly^ OnAssemblyResolve(System::Object^ obj, System::ResolveEventArgs^ args)
{
auto requestedName = gcnew AssemblyName(args->Name);
auto assemblyPath = Path::GetFullPath(Path::Combine("./", requestedName->Name + ".dll"));
if (!File::Exists(assemblyPath)) {
assemblyPath = Path::GetFullPath(Path::Combine("./", requestedName->Name + ".exe"));
if (!File::Exists(assemblyPath))
return nullptr;
}
auto assembly = Assembly::LoadFrom(assemblyPath);
return assembly;
}
void SetUpAssemblyResolve()
{
AppDomain::CurrentDomain->AssemblyResolve += gcnew System::ResolveEventHandler(&OnAssemblyResolve);
}
I created a test-project where you can see the problems. And I also found a solution.
The TestProject can be found here:
https://github.com/relascope/TestingCppClrCs/
This Commit still has problems:
https://github.com/relascope/TestingCppClrCs/commit/c943d806db2deba5470cbb49994371cbbf92331e
Native C++ Tests calling Dll Calling C++/CLR calling C#-assembly fails to load the assembly.
Setting the test project to compile with /clr does not help, the tests disappear. Anyway, this does not matter, since calling into C++/CLR-Dll is possible the same way like calling native code, and it doesn't really matter, if the test-code itself is native C++ or C++/CLR.
/Diag:testdiagnostics.log
shows, that the vstest.console.exe tries to load the assemblies only from some Visual Studio folders.
"./" is not searched.
Since we cannot mock away all C# calls (Solution is too big and has grown over the years and some Interfaces are in C#), it is essential to use it.
One way would be to use google-test (which has no problem loading the C# assemblies), but here we are facing difficulties with Typemocks Isolator++.
The Solution for now is to use AppDomain::CurrentDomain->AssemblyResolve to register a custom AssemblyResolver, which just tries to load from "./".
For the testing spike, this solution is sufficient (can be seen in the latest commit of the TestingCppClrCs Project).
The next step will be the integration into our real project.