25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

80 lines
2.4 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 Moq;
  8. using Xunit;
  9. namespace DotNetCore.CAP.Test
  10. {
  11. public class ConsumerInvokerFactoryTest
  12. {
  13. private IServiceProvider _serviceProvider;
  14. private Mock<IContentSerializer> _mockSerialiser;
  15. private Mock<IMessagePacker> _mockMessagePacker;
  16. private Mock<IModelBinderFactory> _mockModelBinderFactory;
  17. public ConsumerInvokerFactoryTest()
  18. {
  19. _mockSerialiser = new Mock<IContentSerializer>();
  20. _mockMessagePacker = new Mock<IMessagePacker>();
  21. _mockModelBinderFactory = new Mock<IModelBinderFactory>();
  22. var services = new ServiceCollection();
  23. services.AddSingleton<ConsumerInvokerFactory>();
  24. services.AddLogging();
  25. services.AddSingleton(_mockSerialiser.Object);
  26. services.AddSingleton(_mockMessagePacker.Object);
  27. services.AddSingleton(_mockModelBinderFactory.Object);
  28. _serviceProvider = services.BuildServiceProvider();
  29. }
  30. private ConsumerInvokerFactory Create() =>
  31. _serviceProvider.GetService<ConsumerInvokerFactory>();
  32. [Fact]
  33. public void CreateInvokerTest()
  34. {
  35. // Arrange
  36. var fixure = Create();
  37. // Act
  38. var invoker = fixure.CreateInvoker();
  39. // Assert
  40. Assert.NotNull(invoker);
  41. }
  42. [Theory]
  43. [InlineData(nameof(Sample.ThrowException))]
  44. [InlineData(nameof(Sample.AsyncMethod))]
  45. public void InvokeMethodTest(string methodName)
  46. {
  47. // Arrange
  48. var fixure = Create();
  49. var methodInfo = typeof(Sample).GetRuntimeMethods()
  50. .Single(x => x.Name == methodName);
  51. var description = new ConsumerExecutorDescriptor
  52. {
  53. MethodInfo = methodInfo,
  54. ImplTypeInfo = typeof(Sample).GetTypeInfo()
  55. };
  56. var messageContext = new MessageContext();
  57. var context = new ConsumerContext(description, messageContext);
  58. var invoker = fixure.CreateInvoker();
  59. Assert.Throws<Exception>(() =>
  60. {
  61. invoker.InvokeAsync(context).GetAwaiter().GetResult();
  62. });
  63. }
  64. }
  65. }