Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify response mocking in unit tests #392

Merged
merged 2 commits into from
Dec 15, 2023

Conversation

julianlocke
Copy link
Contributor

@julianlocke julianlocke commented Dec 15, 2023

This PR makes mocking SDK responses in test a little more ergonomic. It does this in two ways:

  • It provides a utility for producing a mock SDK namespace (aka a routes object) already backed by a MockTransportClient. This eliminates fiddly few lines that all tests previously needed.
  • It extends the mock input interface to take, in addition to the existing JSON fixture inputs, model object (result or error objects, e.g. (Files.Metadata)) inputs.
func testExample() throws {
    let e = expectation(description: "")

    let (filesRoutes, mockTransportClient) = MockingUtilities.makeRoutesObject(forType: FilesRoutes.self)
    let webService = ExampleWebService(routes: filesRoutes)

    webService.getMetadata { result, _ in
        XCTAssertNotNil(result)
        e.fulfill()
    }

    // Mocking option 1
    let model: Files.Metadata = Files.FileMetadata(
        name: "name", id: "id", clientModified: Date(), serverModified: Date(), rev: "123456789", size: 0
    )
    try mockTransportClient.getLastRequest()?.handleMockInput(
        .success(model: model)
    )

    // Mocking option 2
    let fileMetadataJSON: [String: Any] =
    [
        ".tag": "file",
        "id": "id",
        "server_modified": "2023-12-15T13:43:32Z",
        "name": "name",
        "size": 0,
        "client_modified": "2023-12-15T13:43:32Z",
        "rev": "123456789",
        "is_downloadable": 1,
    ]
    try mockTransportClient.getLastRequest()?.handleMockInput(
        .success(json: fileMetadataJSON)
    )

    wait(for: [e])
}

This PR also guard against a stack overflow when creating a description for an object fails.

Incorporates codegen changes from: dropbox/stone#315

@julianlocke julianlocke changed the base branch from branch_10.0.0-beta to clean-up-test-helpers December 15, 2023 18:47
@julianlocke julianlocke changed the title Simplify test mocking Simplify response mocking in unit tests Dec 15, 2023
Copy link

@jaquish jaquish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

.success(json: fileMetadataJSON)
)

wait(for: [e])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe manually specify short wait time for these, instead of default

}

func testExampleJsonFixture() throws {
let e = expectation(description: "")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add description

@julianlocke julianlocke changed the base branch from clean-up-test-helpers to branch_10.0.0-beta December 15, 2023 22:45
@julianlocke julianlocke merged commit 4b66501 into branch_10.0.0-beta Dec 15, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants