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.
 
 
 

74 line
2.7 KiB

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