Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

73 linhas
2.3 KiB

  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using DotNetCore.CAP.Abstractions;
  5. using DotNetCore.CAP.Internal;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Logging;
  8. using Xunit;
  9. namespace DotNetCore.CAP.Test
  10. {
  11. public class ConsumerInvokerFactoryTest
  12. {
  13. private IConsumerInvokerFactory consumerInvokerFactory;
  14. public ConsumerInvokerFactoryTest()
  15. {
  16. var services = new ServiceCollection();
  17. services.AddLogging();
  18. services.AddSingleton<IContentSerializer, JsonContentSerializer>();
  19. var provider = services.BuildServiceProvider();
  20. var logFactory = provider.GetRequiredService<ILoggerFactory>();
  21. var binder = new ModelBinderFactory();
  22. consumerInvokerFactory = new ConsumerInvokerFactory(logFactory, binder, provider);
  23. }
  24. [Fact]
  25. public void CreateInvokerTest()
  26. {
  27. var methodInfo = typeof(Sample).GetRuntimeMethods()
  28. .Single(x => x.Name == nameof(Sample.ThrowException));
  29. var description = new ConsumerExecutorDescriptor
  30. {
  31. MethodInfo = methodInfo,
  32. ImplTypeInfo = typeof(Sample).GetTypeInfo()
  33. };
  34. var messageContext = new MessageContext();
  35. var context = new ConsumerContext(description, messageContext);
  36. var invoker = consumerInvokerFactory.CreateInvoker(context);
  37. Assert.NotNull(invoker);
  38. }
  39. [Theory]
  40. [InlineData(nameof(Sample.ThrowException))]
  41. [InlineData(nameof(Sample.AsyncMethod))]
  42. public void InvokeMethodTest(string methodName)
  43. {
  44. var methodInfo = typeof(Sample).GetRuntimeMethods()
  45. .Single(x => x.Name == methodName);
  46. var description = new ConsumerExecutorDescriptor
  47. {
  48. MethodInfo = methodInfo,
  49. ImplTypeInfo = typeof(Sample).GetTypeInfo()
  50. };
  51. var messageContext = new MessageContext();
  52. var context = new ConsumerContext(description, messageContext);
  53. var invoker = consumerInvokerFactory.CreateInvoker(context);
  54. Assert.Throws<Exception>(() =>
  55. {
  56. invoker.InvokeAsync().GetAwaiter().GetResult();
  57. });
  58. }
  59. }
  60. }