You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

73 line
2.9 KiB

  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Microsoft.Extensions.Logging;
  7. using Moq;
  8. using Xunit;
  9. namespace Cap.Consistency.Test
  10. {
  11. public class ConsistencyMessageManagerTest
  12. {
  13. [Fact]
  14. public void EnsureDefaultServicesDefaultsWithStoreWorks() {
  15. var services = new ServiceCollection()
  16. .AddTransient<IConsistencyMessageStore<TestConsistencyMessage>, NoopMessageStore>();
  17. services.AddConsistency<TestConsistencyMessage>();
  18. services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  19. services.AddLogging();
  20. var manager = services.BuildServiceProvider()
  21. .GetRequiredService<ConsistencyMessageManager<TestConsistencyMessage>>();
  22. Assert.NotNull(manager);
  23. }
  24. [Fact]
  25. public void AddMessageManagerWithCustomerMannagerReturnsSameInstance() {
  26. var services = new ServiceCollection()
  27. .AddTransient<IConsistencyMessageStore<TestConsistencyMessage>, NoopMessageStore>()
  28. .AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  29. services.AddLogging();
  30. services.AddConsistency<TestConsistencyMessage>()
  31. .AddConsistencyMessageManager<CustomMessageManager>();
  32. var provider = services.BuildServiceProvider();
  33. Assert.Same(provider.GetRequiredService<ConsistencyMessageManager<TestConsistencyMessage>>(),
  34. provider.GetRequiredService<CustomMessageManager>());
  35. }
  36. public class CustomMessageManager : ConsistencyMessageManager<TestConsistencyMessage>
  37. {
  38. public CustomMessageManager()
  39. : base(new Mock<IConsistencyMessageStore<TestConsistencyMessage>>().Object, null, null) {
  40. }
  41. }
  42. [Fact]
  43. public async Task CreateCallsStore() {
  44. var store = new Mock<IConsistencyMessageStore<TestConsistencyMessage>>();
  45. var message = new TestConsistencyMessage { Time = DateTime.Now };
  46. store.Setup(x => x.CreateAsync(message, CancellationToken.None)).ReturnsAsync(OperateResult.Success).Verifiable();
  47. var messageManager = TestConsistencyMessageManager(store.Object);
  48. var result = await messageManager.CreateAsync(message);
  49. Assert.True(result.Succeeded);
  50. store.VerifyAll();
  51. }
  52. public ConsistencyMessageManager<TMessage> TestConsistencyMessageManager<TMessage>(IConsistencyMessageStore<TMessage> store = null)
  53. where TMessage : class {
  54. store = store ?? new Mock<IConsistencyMessageStore<TMessage>>().Object;
  55. var mockLogger = new Mock<ILogger<ConsistencyMessageManager<TMessage>>>().Object;
  56. var manager = new ConsistencyMessageManager<TMessage>(store, null, mockLogger);
  57. return manager;
  58. }
  59. }
  60. }