Net.Testing.NSubstitute.Grpc 1.0.0

dotnet add package Net.Testing.NSubstitute.Grpc --version 1.0.0                
NuGet\Install-Package Net.Testing.NSubstitute.Grpc -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Net.Testing.NSubstitute.Grpc" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Net.Testing.NSubstitute.Grpc --version 1.0.0                
#r "nuget: Net.Testing.NSubstitute.Grpc, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Net.Testing.NSubstitute.Grpc as a Cake Addin
#addin nuget:?package=Net.Testing.NSubstitute.Grpc&version=1.0.0

// Install Net.Testing.NSubstitute.Grpc as a Cake Tool
#tool nuget:?package=Net.Testing.NSubstitute.Grpc&version=1.0.0                

grpc.net.testing.nsubstitute

.NET

Library to mocking gRPC client. Instead of Grpc.Core.Testing using extensions for NSubstitute

Based on libraries:

Install

Nuget:

Install-Package Net.Testing.NSubstitute.Grpc

Using example

Simple sync calling:

1. Call with exists response
// Create client
var grpcMock = Substitute.For<TestService.TestServiceClient>();

// Add mock
grpcMock
    .Simple(Arg.Any<TestRequest>(), null, null, default)
    .Returns(expectedResponse);

// Call
var response = grpcMock.Simple(new TestRequest());

Simple async calling:

1. Call with exists response
// Create client
var testRequest = new TestRequest();
var grpcClient = Substitute.For<TestService.TestServiceClient>();

// Add mock
grpcClient
    .SimpleAsync(Arg.Any<TestRequest>(), Arg.Any<Metadata>(), Arg.Any<DateTime?>(), Arg.Any<CancellationToken>())
    .Returns(new TestResponse());

// Call
var response = await grpcClient.SimpleAsync(testRequest);
2. Call with response creating on call
// Create client
var testRequest = new TestRequest();
var grpcClient = Substitute.For<TestService.TestServiceClient>();

// Add mock
grpcClient
    .SimpleAsync(Arg.Any<TestRequest>(), Arg.Any<Metadata>(), Arg.Any<DateTime?>(), Arg.Any<CancellationToken>())
    .Returns(() => new TestResponse());

// Call
var response = await grpcClient.SimpleAsync(testRequest);
3. Call with response creating on call and based on request
// Create client
var testRequest = new TestRequest();
var grpcClient = Substitute.For<TestService.TestServiceClient>();

// Add mock
grpcClient
    .SimpleAsync(Arg.Any<TestRequest>(), Arg.Any<Metadata>(), Arg.Any<DateTime?>(), Arg.Any<CancellationToken>())
    .Returns<TestRequest, TestResponse>(r => new TestResponse{ Val = r.Val });

// Call
var response = await grpcClient.SimpleAsync(testRequest);

Simple client stream calling:

1. Call with exists response
// Create client
var grpcMock = Substitute.For<TestService.TestServiceClient>();

// Add mock
grpcMock
    .SimpleClientStream(Arg.Any<Metadata>(), Arg.Any<DateTime?>(), Arg.Any<CancellationToken>())
    .Returns(expectedResponses);

// Call
var call = grpcMock.SimpleClientStream();

await call.RequestStream.WriteAllAsync(requests, complete: true);
var message = await call;
2. Call with response creating on call
// Create client
var grpcMock = Substitute.For<TestService.TestServiceClient>();

// Add mock
grpcMock
    .SimpleClientStream(Arg.Any<Metadata>(), Arg.Any<DateTime?>(), Arg.Any<CancellationToken>())
    .Returns(() => expectedResponses);

// Call
var call = grpcMock.SimpleClientStream();

await call.RequestStream.WriteAllAsync(requests, complete: true);
var message = await call;
3. Call with response creating on call and based on request
// Create client
var grpcMock = Substitute.For<TestService.TestServiceClient>();

// Add mock
grpcMock
    .SimpleClientStream(Arg.Any<Metadata>(), Arg.Any<DateTime?>(), Arg.Any<CancellationToken>())
    .Returns(rs => new TestResponse { Val = rs.Sum(r => r.Val) });

// Call
var call = grpcMock.SimpleClientStream();

await call.RequestStream.WriteAllAsync(requests, complete: true);
var message = await call;

Simple server stream calling:

1. Call with exists response
// Create client
var grpcMock = Substitute.For<TestService.TestServiceClient>();

// Add mock
grpcMock
    .SimpleServerStream(Arg.Any<TestRequest>(), null, null, default)
    .Returns(expectedResponses);

// Call
var call = grpcMock.SimpleServerStream(new TestRequest());
var messages = await call.ResponseStream.ReadAllAsync().ToArrayAsync();
2. Call with response creating on call
// Create client
var grpcMock = Substitute.For<TestService.TestServiceClient>();

// Add mock
grpcMock
    .SimpleServerStream(Arg.Any<TestRequest>(), null, null, default)
    .Returns(() => expectedResponses);

// Call
var call = grpcMock.SimpleServerStream(new TestRequest());
var messages = await call.ResponseStream.ReadAllAsync().ToArrayAsync();
3. Call with response creating on call and based on request
// Create client
var grpcMock = Substitute.For<TestService.TestServiceClient>();

// Add mock
grpcMock
    .SimpleServerStream(Arg.Any<TestRequest>(), null, null, default)
    .Returns<TestRequest, TestResponse>(r => Enumerable.Repeat(new TestResponse { Val = r.Val }, 2));

// Call
var call = grpcMock.SimpleServerStream(new TestRequest());
var messages = await call.ResponseStream.ReadAllAsync().ToArrayAsync();

Simple client server streams calling:

1. Call with exists response
// Creation client
var grpcMock = Substitute.For<TestService.TestServiceClient>();

// Add mock
grpcMock
    .SimpleClientServerStream(null, null, default)
    .Returns(expectedResponses);

// Call
var call = grpcMock.SimpleClientServerStream();

await call.RequestStream.WriteAllAsync(requests, complete: true);
var messages = await call.ResponseStream.ReadAllAsync().ToArrayAsync();
2. Call with response creating on call
// Creation client
var grpcMock = Substitute.For<TestService.TestServiceClient>();

// Add mock
grpcMock
    .SimpleClientServerStream(null, null, default)
    .Returns(() => expectedResponses);

// Call
var call = grpcMock.SimpleClientServerStream();

await call.RequestStream.WriteAllAsync(requests, complete: true);
var messages = await call.ResponseStream.ReadAllAsync().ToArrayAsync();
3. Call with response creating on call and based on request
// Creation client
var grpcMock = Substitute.For<TestService.TestServiceClient>();

// Add mock
grpcMock
    .SimpleClientServerStream(null, null, default)
    .Returns(rs => new[] { new TestResponse { Val = rs.Sum(r => r.Val) } });

// Call
var call = grpcMock.SimpleClientServerStream();

await call.RequestStream.WriteAllAsync(requests, complete: true);
var messages = await call.ResponseStream.ReadAllAsync().ToArrayAsync();
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 324 10/1/2023