MathClientMockableTest.cs 4.7 KB

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