diff --git a/src/csharp/Grpc.Core.Testing/TestCalls.cs b/src/csharp/Grpc.Core.Testing/TestCalls.cs index ac29a8b9741..8c76781bbe3 100644 --- a/src/csharp/Grpc.Core.Testing/TestCalls.cs +++ b/src/csharp/Grpc.Core.Testing/TestCalls.cs @@ -65,7 +65,7 @@ namespace Grpc.Core.Testing /// Creates a test double for AsyncDuplexStreamingCall. Only for testing. /// Note: experimental API that can change or be removed without any prior notice. /// - public static AsyncDuplexStreamingCall AsyncDuplexStreamingCall( + public static AsyncDuplexStreamingCall AsyncDuplexStreamingCall( IClientStreamWriter requestStream, IAsyncStreamReader responseStream, Task responseHeadersAsync, Func getStatusFunc, Func getTrailersFunc, Action disposeAction) diff --git a/src/csharp/Grpc.Examples.Tests/Grpc.Examples.Tests.csproj b/src/csharp/Grpc.Examples.Tests/Grpc.Examples.Tests.csproj index 3ccc9adfaf7..d2cc5bbc65e 100755 --- a/src/csharp/Grpc.Examples.Tests/Grpc.Examples.Tests.csproj +++ b/src/csharp/Grpc.Examples.Tests/Grpc.Examples.Tests.csproj @@ -13,11 +13,13 @@ + + diff --git a/src/csharp/Grpc.Examples.Tests/MathClientMockableTest.cs b/src/csharp/Grpc.Examples.Tests/MathClientMockableTest.cs new file mode 100644 index 00000000000..6ed7b0a9374 --- /dev/null +++ b/src/csharp/Grpc.Examples.Tests/MathClientMockableTest.cs @@ -0,0 +1,101 @@ +#region Copyright notice and license + +// Copyright 2018 The gRPC Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#endregion + +using System; +using System.Threading; +using System.Threading.Tasks; +using Grpc.Core; +using Grpc.Core.Testing; +using NUnit.Framework; + +namespace Math.Tests +{ + /// + /// Demonstrates how to mock method stubs for all method types in a generated client. + /// + public class MathClientMockableTest + { + [Test] + public void ClientBaseBlockingUnaryCallCanBeMocked() + { + var mockClient = new Moq.Mock(); + + var expected = new DivReply(); + mockClient.Setup(m => m.Div(Moq.It.IsAny(), null, null, CancellationToken.None)).Returns(expected); + Assert.AreSame(expected, mockClient.Object.Div(new DivArgs())); + } + + [Test] + public void ClientBaseBlockingUnaryCallWithCallOptionsCallCanBeMocked() + { + var mockClient = new Moq.Mock(); + + var expected = new DivReply(); + mockClient.Setup(m => m.Div(Moq.It.IsAny(), Moq.It.IsAny())).Returns(expected); + Assert.AreSame(expected, mockClient.Object.Div(new DivArgs(), new CallOptions())); + } + + [Test] + public void ClientBaseAsyncUnaryCallCanBeMocked() + { + var mockClient = new Moq.Mock(); + + // Use a factory method provided by Grpc.Core.Testing.TestCalls to create an instance of a call. + var fakeCall = TestCalls.AsyncUnaryCall(Task.FromResult(new DivReply()), Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { }); + mockClient.Setup(m => m.DivAsync(Moq.It.IsAny(), null, null, CancellationToken.None)).Returns(fakeCall); + Assert.AreSame(fakeCall, mockClient.Object.DivAsync(new DivArgs())); + } + + [Test] + public void ClientBaseClientStreamingCallCanBeMocked() + { + var mockClient = new Moq.Mock(); + var mockRequestStream = new Moq.Mock>(); + + // Use a factory method provided by Grpc.Core.Testing.TestCalls to create an instance of a call. + var fakeCall = TestCalls.AsyncClientStreamingCall(mockRequestStream.Object, Task.FromResult(new Num()), Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { }); + mockClient.Setup(m => m.Sum(null, null, CancellationToken.None)).Returns(fakeCall); + Assert.AreSame(fakeCall, mockClient.Object.Sum()); + } + + [Test] + public void ClientBaseServerStreamingCallCanBeMocked() + { + var mockClient = new Moq.Mock(); + var mockResponseStream = new Moq.Mock>(); + + // Use a factory method provided by Grpc.Core.Testing.TestCalls to create an instance of a call. + var fakeCall = TestCalls.AsyncServerStreamingCall(mockResponseStream.Object, Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { }); + mockClient.Setup(m => m.Fib(Moq.It.IsAny(), null, null, CancellationToken.None)).Returns(fakeCall); + Assert.AreSame(fakeCall, mockClient.Object.Fib(new FibArgs())); + } + + [Test] + public void ClientBaseDuplexStreamingCallCanBeMocked() + { + var mockClient = new Moq.Mock(); + var mockRequestStream = new Moq.Mock>(); + var mockResponseStream = new Moq.Mock>(); + + // Use a factory method provided by Grpc.Core.Testing.TestCalls to create an instance of a call. + var fakeCall = TestCalls.AsyncDuplexStreamingCall(mockRequestStream.Object, mockResponseStream.Object, Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { }); + mockClient.Setup(m => m.DivMany(null, null, CancellationToken.None)).Returns(fakeCall); + Assert.AreSame(fakeCall, mockClient.Object.DivMany()); + } + } +} diff --git a/src/csharp/Grpc.IntegrationTesting/GeneratedClientTest.cs b/src/csharp/Grpc.IntegrationTesting/GeneratedClientTest.cs index 24626068791..c8bcb7a93f1 100644 --- a/src/csharp/Grpc.IntegrationTesting/GeneratedClientTest.cs +++ b/src/csharp/Grpc.IntegrationTesting/GeneratedClientTest.cs @@ -33,29 +33,6 @@ namespace Grpc.IntegrationTesting { TestService.TestServiceClient unimplementedClient = new UnimplementedTestServiceClient(); - [Test] - public void ExpandedParamOverloadCanBeMocked() - { - var expected = new SimpleResponse(); - - var mockClient = new Moq.Mock(); - // mocking is relatively clumsy because one needs to specify value for all the optional params. - mockClient.Setup(m => m.UnaryCall(Moq.It.IsAny(), null, null, CancellationToken.None)).Returns(expected); - - Assert.AreSame(expected, mockClient.Object.UnaryCall(new SimpleRequest())); - } - - [Test] - public void CallOptionsOverloadCanBeMocked() - { - var expected = new SimpleResponse(); - - var mockClient = new Moq.Mock(); - mockClient.Setup(m => m.UnaryCall(Moq.It.IsAny(), Moq.It.IsAny())).Returns(expected); - - Assert.AreSame(expected, mockClient.Object.UnaryCall(new SimpleRequest(), new CallOptions())); - } - [Test] public void DefaultMethodStubThrows_UnaryCall() { diff --git a/src/csharp/Grpc.IntegrationTesting/Grpc.IntegrationTesting.csproj b/src/csharp/Grpc.IntegrationTesting/Grpc.IntegrationTesting.csproj index ba2107a5769..e4f36d8810d 100755 --- a/src/csharp/Grpc.IntegrationTesting/Grpc.IntegrationTesting.csproj +++ b/src/csharp/Grpc.IntegrationTesting/Grpc.IntegrationTesting.csproj @@ -19,7 +19,6 @@ - diff --git a/src/csharp/tests.json b/src/csharp/tests.json index 60f67ff3c91..dc1f0857952 100644 --- a/src/csharp/tests.json +++ b/src/csharp/tests.json @@ -41,6 +41,7 @@ "Grpc.Core.Tests.UserAgentStringTest" ], "Grpc.Examples.Tests": [ + "Math.Tests.MathClientMockableTest", "Math.Tests.MathClientServerTest" ], "Grpc.HealthCheck.Tests": [