WebApplicationFactory for Multiple Interconnected web applications

Chandresh Sanghavi 1 Reputation point
2022-04-07T17:43:59.96+00:00

Hello,

I almost have similar scenarios (like the link below)... Let me explain first:

I have WebAPI Application1 which has DoSomethingUsingBarApi() to communicate to WebApi of Application 2. I want to test the round trip.

1) My First Web Application MyWebApi1
// MyWebApi1.csproj has <InternalsVisibleTo Include="MyTestProject" />
namespace MyWebApi1 {
// Program File with following declaration
// public partial class MyWebApi1Program {};

[ApiController]
public class FooApi : ApiControllerBase {

public bool DoSomethingUsingBarApi()
{
var httpResponseMessage = myHttpClient.PostAsJsonAsync("../BarApiHelloMethod").Result;
return httpResponseMessage.EnsureSuccessStatusCode();
}
} // End of Namespace MyWebApi1

2) My Second Web application - MyWebApi2
// MyWebApi1.csproj has <InternalsVisibleTo Include="MyTestProject" />
namespace MyWebApi2 {
// Program File with following declaration
// public partial class MyWebApi2Program {};
[ApiController]
public class BarApi : ApiControllerBase {

public string BarApiHelloMethod()
{
return "Hello";
}
} // End of Namespace MyWebApi2

3) My Test Project - MyTestProject
// Test application has two WebApplicationFactory with MyWebApi1Program and MyWebApi2Program
namespace MyTestProject {
[TestFixture]
public class MyTestClass {
[Test]
public MyTestMethod1() {
var factory1 = new WebApplicationFactory<MyWebApi1Program>() ...... ;
var client1 = factory1.CreateClient();
client1.BaseAddress = "http://localhost:SomePort";

         var factory2 = new WebApplicationFactory<MyWebApi2Program>().....;  
         var client2 = factory2.CreateClient();  

        var result = client1.PostAsJsonAsync("/DoSomethingUsingBarApi").result;  
        ////// My issue is below ////////////  
        // Note - The debugger control lends in the DoSmethingUsingBarApi .. but it returns 500 server error   
        // below Assert fails  
       // Note - If I run the MyWebApi2 in another VS instance and run the above method, all work fine  
       // Somehow, the issue is that Request sent using in memory   
        // factory1 using client1 from MyWebApi1 to MyWebApi2 is not finding MyWebApi2  
        Assert.AreEqual(System.Net.HttpStatusCode.OK, result.StatusCode);  
 }  

} // End of MyTestProject

I looked at following link for the answer to my question but it does not help.
https://learn.microsoft.com/en-us/answers/questions/783967/integration-test-in-net6.html

I assume that Factory2, the MyWebApi2Program and its assemblies are loaded.

Please note that I have just put the main code snippet here. So, ignore all other requirements and look into this description with assumption that both Api Projects and a Test project are compiling, building and running fine along with some success to entering inside first API project.

I am stuck here and any help or suggestion are greatly appreciated.

Thanks in Advance for looking into this.
-Chandresh

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,177 questions
Visual Studio Testing
Visual Studio Testing
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Testing: The act or process of applying tests as a means of analysis or diagnosis.
329 questions
0 comments No comments
{count} votes