您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

74 行
3.0 KiB

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