saloon icon indicating copy to clipboard operation
saloon copied to clipboard

Possible to Use Multiple Instances of Same Request in Single Test?

Open chrismcintosh opened this issue 1 year ago • 0 comments

I'm sure there are much smarter ways to go about what I'm trying to do. I've reviewed the documentation and I can't find what I need.

        MockClient::global([
           // ❌ DOES NOT GET USED
           NetworkCreatePrivateRequest::class => MockResponse::fixture(
                "create_private_network_request"
            ),
            // ✅ GETS USED FOR BOTH NETWORK CREATE
            NetworkCreatePrivateRequest::class => MockResponse::fixture(
                "create_vm_network_request"
            ),
           // ❌ DOES NOT GET USED
            CreateServer::class => MockResponse::fixture(
                "create_server_1"
            ),
           // ❌ DOES NOT GET USED
            CreateServer::class => MockResponse::fixture(
                "create_server_2"
            ),
           // ✅ GETS USED FOR ALL 3 CREATE SERVER
            CreateServer::class => MockResponse::fixture(
                "create_server_3"
            ),
        ]);

I have a test where I use the same request classes multiple times. Each time the request class is used it needs to respond with data from a different fixture to mimic what the code is doing.

I assumed the MockClient would use the first instance of a request class encountered and after it is used once it looks for the next instance in the MockClient but that is not how it works.

The problem I am running into is that it seems that only the last instances in the MockClient::global array are used.

  • In my example above I have 2 network create requests
    • Both network requests end up using the fixture "create_vm_network_request"
  • In my example above I have 3 server create requests.
    • All 3 create server requests end up using the "create_server_3" fixture.

Is it possible to tailor these calls in order with responses to have different fixtures/data - or is there another way to achieve this?

I would like to avoid create a separate class for each possible instance of network or server because the requests are so similar - but the responses need to be different.

Best, Chris

chrismcintosh avatar Jun 12 '24 18:06 chrismcintosh