grpc / grpc/grpc-rust

Looking for unit test/mock guidance

Open
#2,335 3 comments 1 reaction 0 assignees View on GitHub
Dominant language
Rust
Stars
12.5k
Forks
1.3k
Avg merge
4d 7h
Merged PRs (30d)
24

Description

Hi,

I'm looking for an example on how to unit test/mock gRPC responses for a client. For instance, I have the following code:
```rust
async fn main() -> Result<()> {
let client = GrpcClient::connect(...);
grpc_call(client).await?;
}

async fn grpc_call(mut client: GrpcClient) -> Result<()> {
let result = client.grpc_call(Request::new(GrpcRequest {/* valid input */})).await?;

// do something with result

Ok(())
}
```

Now I would like to do something like this:
```rust
#[cfg(test)]
mod tests {

#[test]
fn test_grpc_call() {

// setup mock client, with
// code for asserting that the request has the correct fields set, and
// return a mock response

grpc_call(mock_client).expect("should work");
}
}
````

I can't find any examples on how to setup any kind of mocking/testing generated clients. Is anyone doing this in their repo, and if so can you please point me to a working example?

I've looked at https://github.com/hyperium/tonic/blob/master/examples/src/mock/mock.rs, but that doesn't have any tests so I'm insure how to use it (if it is even possible to do what I want with that example).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.