GeneratedClientTest.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #region Copyright notice and license
  2. // Copyright 2015-2016, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #endregion
  31. using System;
  32. using System.Collections.Generic;
  33. using System.IO;
  34. using System.Linq;
  35. using System.Threading;
  36. using System.Threading.Tasks;
  37. using Grpc.Core;
  38. using Grpc.Core.Utils;
  39. using Grpc.Testing;
  40. using NUnit.Framework;
  41. namespace Grpc.IntegrationTesting
  42. {
  43. public class GeneratedClientTest
  44. {
  45. TestService.TestServiceClient unimplementedClient = new UnimplementedTestServiceClient();
  46. [Test]
  47. public void ExpandedParamOverloadCanBeMocked()
  48. {
  49. var expected = new SimpleResponse();
  50. var mockClient = new Moq.Mock<TestService.TestServiceClient>();
  51. // mocking is relatively clumsy because one needs to specify value for all the optional params.
  52. mockClient.Setup(m => m.UnaryCall(Moq.It.IsAny<SimpleRequest>(), null, null, CancellationToken.None)).Returns(expected);
  53. Assert.AreSame(expected, mockClient.Object.UnaryCall(new SimpleRequest()));
  54. }
  55. [Test]
  56. public void CallOptionsOverloadCanBeMocked()
  57. {
  58. var expected = new SimpleResponse();
  59. var mockClient = new Moq.Mock<TestService.TestServiceClient>();
  60. mockClient.Setup(m => m.UnaryCall(Moq.It.IsAny<SimpleRequest>(), Moq.It.IsAny<CallOptions>())).Returns(expected);
  61. Assert.AreSame(expected, mockClient.Object.UnaryCall(new SimpleRequest(), new CallOptions()));
  62. }
  63. [Test]
  64. public void DefaultMethodStubThrows_UnaryCall()
  65. {
  66. Assert.Throws(typeof(NotImplementedException), () => unimplementedClient.UnaryCall(new SimpleRequest()));
  67. }
  68. [Test]
  69. public void DefaultMethodStubThrows_ClientStreaming()
  70. {
  71. Assert.Throws(typeof(NotImplementedException), () => unimplementedClient.StreamingInputCall());
  72. }
  73. [Test]
  74. public void DefaultMethodStubThrows_ServerStreaming()
  75. {
  76. Assert.Throws(typeof(NotImplementedException), () => unimplementedClient.StreamingOutputCall(new StreamingOutputCallRequest()));
  77. }
  78. [Test]
  79. public void DefaultMethodStubThrows_DuplexStreaming()
  80. {
  81. Assert.Throws(typeof(NotImplementedException), () => unimplementedClient.FullDuplexCall());
  82. }
  83. /// <summary>
  84. /// Subclass of the generated client that doesn't override any method stubs.
  85. /// </summary>
  86. private class UnimplementedTestServiceClient : TestService.TestServiceClient
  87. {
  88. }
  89. }
  90. }