MockPostApiBaseTest.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using Newtonsoft.Json;
  2. using System;
  3. using JCSoft.WX.Framework.Models.ApiRequests;
  4. using JCSoft.WX.Framework.Models.ApiResponses;
  5. using Xunit;
  6. using System.Reflection;
  7. using System.Threading.Tasks;
  8. namespace FrameworkCoreTest
  9. {
  10. public abstract class MockPostApiBaseTest<TRequest, TResponse> : BaseTest
  11. where TRequest : ApiRequest<TResponse>
  12. where TResponse : ApiResponse, new()
  13. {
  14. protected static string s_errmsg = "{\"errcode\":40007,\"errmsg\":\"invalid media_id\"}";
  15. protected static string s_successmsg = "{\"errcode\":0,\"errmsg\":\"success\"}";
  16. private TRequest m_request = null;
  17. public TRequest Request
  18. {
  19. get
  20. {
  21. if (m_request == null)
  22. {
  23. m_request = InitRequestObject();
  24. //m_request.Logger = new Logger();
  25. }
  26. return m_request;
  27. }
  28. }
  29. protected abstract TRequest InitRequestObject();
  30. protected bool IsMock { get; set; } = true;
  31. public void MockSetup(bool errResult)
  32. {
  33. mock_client.Setup(d => d.Execute<TResponse>(Request)).Returns(Deserialzer(GetReturnResult(errResult)));
  34. }
  35. protected abstract string GetReturnResult(bool errResult);
  36. public override string GetCurrentToken()
  37. {
  38. if (IsMock)
  39. return "123";
  40. return base.GetCurrentToken();
  41. }
  42. //[Fact]
  43. //public virtual void MockGetPostContent()
  44. //{
  45. // Console.WriteLine(Request.GetPostContent());
  46. //}
  47. //[Fact]
  48. //public virtual void MockResponseTypeTest()
  49. //{
  50. // MockSetup(false);
  51. // var response = GetResponse();
  52. // Assert.IsType<TResponse>(response);
  53. // var pro = response.GetType().GetProperties();
  54. // foreach (var p in pro)
  55. // {
  56. // Console.WriteLine("{0}:{1}", p.Name, JsonConvert.SerializeObject(p.GetValue(response)));
  57. // }
  58. //}
  59. public virtual TResponse GetResponse()
  60. {
  61. throw new NotImplementedException();
  62. }
  63. protected string JsonSerialize(object obj)
  64. {
  65. return JsonConvert.SerializeObject(obj);
  66. }
  67. protected TResponse Deserialzer(string result)
  68. {
  69. return JsonConvert.DeserializeObject<TResponse>(result);
  70. }
  71. }
  72. }