Skip to content

Latest commit

 

History

History
76 lines (58 loc) · 4.25 KB

README.MD

File metadata and controls

76 lines (58 loc) · 4.25 KB

Mocking HTTP Requests in Unity for Unit Tests

The overall objective is to mock the UnityWebRequest.

We found a solution to mock HTTP requests in Unity to write unit test, with the help of Nsubstitute and a wrapper interface.

To test the UnityWebRequest, we use two interfaces to wrap the original UnityWebRequest and the UploadHandler. These interfaces are IGoedleUploadHandler and IGoedleWebRequest.

With these interfaces we are able to mock:

  • Response Codes
  • Content
  • Header
  • HTTP errors
  • Networks errors
  • Request methods

Therefore, we created a basic HTTP client in Unity to test a UnityWebRequest. In the unit test we can now mock UnityWebRequest attributes and access passed content. The following example shows how to mock a web request and validate the send JSON content and HTTP errors:

Example

You find in this repository a minimal full running example, which can be opened as Unity project and directly execute with the Unity Test Runner.

Test

[Test]
public void checkBehaviorWebRequestSendPost()
{
    BasicHttpClient http_client = (new GameObject("BasicHttpClient")).AddComponent();
    IGoedleUploadHandler guh = Substitute.For();
    IGoedleWebRequest gw = Substitute.For();
    string stringContent = null;
    // Here we write the value which is passed through the add function to stringContent
    guh.add(Arg.Do(x => stringContent = x));
    gw.isHttpError.Returns(false);
    gw.isNetworkError.Returns(false);
    http_client.sendPost("{\"test\": \"test\"}", gw, guh);
    var result = JSON.Parse(stringContent);
    Assert.AreEqual(result["test"].Value, "test");
}

Directory Structure

├───Assets/
│   ├───goedle_io/
│   │   ├───Editor/
│   │   │   ├───Tests/
│   │   │   │   ├───TrackRequestTest.cs
│   │   │   ├───NSubstitute.dll
│   │   ├───Plugins/
│   │   │   ├───SimpleJSON.cs
│   │   ├───Scripts/
│   │   │   ├───detail/
│   │   │   │   ├───BasicHttpClient.cs
│   │   │   │   ├───GoedleUploadHandler.cs
│   │   │   │   ├───GoedleWebRequest.cs
  • TrackRequestTest.cs -> Holds an example unit test
  • BasicHttpClient.cs -> Holds the HTTP client that we want to test
  • GoedleUploadHandler.cs -> Holds the interface for wrapped UploadHandler
  • GoedleWebRequest.cs -> Holds the interface for the wrapped UnityWebRequest

When you open this project in Unity you directly run the Unity Test Runner.

Unity Test Runner

Additional Information and Links

To create and validate JSON we are making use of the SIMPLEJSON script.

We are also using these testing framework in the goedle.io Unity SDK. You can also access the goedle.io Unity SDK via the RAGE Platform and you find a hands-on-example of on how to use the SDK here.

The blog post Fake it until you make it contains more detailed information and a description on how to create unit test for the UnityWebRequest.