• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2018 The gRPC Authors
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #endregion
18 
19 using System;
20 using System.Threading;
21 using System.Threading.Tasks;
22 using Grpc.Core;
23 using Grpc.Core.Testing;
24 using NUnit.Framework;
25 
26 namespace Math.Tests
27 {
28     /// <summary>
29     /// Demonstrates how to mock method stubs for all method types in a generated client.
30     /// </summary>
31     public class MathClientMockableTest
32     {
33         [Test]
ClientBaseBlockingUnaryCallCanBeMocked()34         public void ClientBaseBlockingUnaryCallCanBeMocked()
35         {
36             var mockClient = new Moq.Mock<Math.MathClient>();
37 
38             var expected = new DivReply();
39             mockClient.Setup(m => m.Div(Moq.It.IsAny<DivArgs>(), null, null, CancellationToken.None)).Returns(expected);
40             Assert.AreSame(expected, mockClient.Object.Div(new DivArgs()));
41         }
42 
43         [Test]
ClientBaseBlockingUnaryCallWithCallOptionsCallCanBeMocked()44         public void ClientBaseBlockingUnaryCallWithCallOptionsCallCanBeMocked()
45         {
46             var mockClient = new Moq.Mock<Math.MathClient>();
47 
48             var expected = new DivReply();
49             mockClient.Setup(m => m.Div(Moq.It.IsAny<DivArgs>(), Moq.It.IsAny<CallOptions>())).Returns(expected);
50             Assert.AreSame(expected, mockClient.Object.Div(new DivArgs(), new CallOptions()));
51         }
52 
53         [Test]
ClientBaseAsyncUnaryCallCanBeMocked()54         public void ClientBaseAsyncUnaryCallCanBeMocked()
55         {
56             var mockClient = new Moq.Mock<Math.MathClient>();
57 
58             // Use a factory method provided by Grpc.Core.Testing.TestCalls to create an instance of a call.
59             var fakeCall = TestCalls.AsyncUnaryCall<DivReply>(Task.FromResult(new DivReply()), Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { });
60             mockClient.Setup(m => m.DivAsync(Moq.It.IsAny<DivArgs>(), null, null, CancellationToken.None)).Returns(fakeCall);
61             Assert.AreSame(fakeCall, mockClient.Object.DivAsync(new DivArgs()));
62         }
63 
64         [Test]
ClientBaseClientStreamingCallCanBeMocked()65         public void ClientBaseClientStreamingCallCanBeMocked()
66         {
67             var mockClient = new Moq.Mock<Math.MathClient>();
68             var mockRequestStream = new Moq.Mock<IClientStreamWriter<Num>>();
69 
70             // Use a factory method provided by Grpc.Core.Testing.TestCalls to create an instance of a call.
71             var fakeCall = TestCalls.AsyncClientStreamingCall<Num, Num>(mockRequestStream.Object, Task.FromResult(new Num()), Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { });
72             mockClient.Setup(m => m.Sum(null, null, CancellationToken.None)).Returns(fakeCall);
73             Assert.AreSame(fakeCall, mockClient.Object.Sum());
74         }
75 
76         [Test]
ClientBaseServerStreamingCallCanBeMocked()77         public void ClientBaseServerStreamingCallCanBeMocked()
78         {
79             var mockClient = new Moq.Mock<Math.MathClient>();
80             var mockResponseStream = new Moq.Mock<IAsyncStreamReader<Num>>();
81 
82             // Use a factory method provided by Grpc.Core.Testing.TestCalls to create an instance of a call.
83             var fakeCall = TestCalls.AsyncServerStreamingCall<Num>(mockResponseStream.Object, Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { });
84             mockClient.Setup(m => m.Fib(Moq.It.IsAny<FibArgs>(), null, null, CancellationToken.None)).Returns(fakeCall);
85             Assert.AreSame(fakeCall, mockClient.Object.Fib(new FibArgs()));
86         }
87 
88         [Test]
ClientBaseDuplexStreamingCallCanBeMocked()89         public void ClientBaseDuplexStreamingCallCanBeMocked()
90         {
91             var mockClient = new Moq.Mock<Math.MathClient>();
92             var mockRequestStream = new Moq.Mock<IClientStreamWriter<DivArgs>>();
93             var mockResponseStream = new Moq.Mock<IAsyncStreamReader<DivReply>>();
94 
95             // Use a factory method provided by Grpc.Core.Testing.TestCalls to create an instance of a call.
96             var fakeCall = TestCalls.AsyncDuplexStreamingCall<DivArgs, DivReply>(mockRequestStream.Object, mockResponseStream.Object, Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { });
97             mockClient.Setup(m => m.DivMany(null, null, CancellationToken.None)).Returns(fakeCall);
98             Assert.AreSame(fakeCall, mockClient.Object.DivMany());
99         }
100     }
101 }
102