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.
 
 
 

88 linhas
2.5 KiB

  1. using System;
  2. using System.Threading.Tasks;
  3. using DotNetCore.CAP.Abstractions;
  4. using DotNetCore.CAP.Internal;
  5. using DotNetCore.CAP.Models;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Moq;
  8. using Xunit;
  9. namespace DotNetCore.CAP.Test
  10. {
  11. public class CallbackMessageSenderTest
  12. {
  13. private IServiceProvider _provider;
  14. private Mock<ICallbackPublisher> _mockCallbackPublisher;
  15. private Mock<IContentSerializer> _mockContentSerializer;
  16. private Mock<IMessagePacker> _mockMessagePack;
  17. public CallbackMessageSenderTest()
  18. {
  19. _mockCallbackPublisher = new Mock<ICallbackPublisher>();
  20. _mockContentSerializer = new Mock<IContentSerializer>();
  21. _mockMessagePack = new Mock<IMessagePacker>();
  22. var services = new ServiceCollection();
  23. services.AddTransient<CallbackMessageSender>();
  24. services.AddLogging();
  25. services.AddSingleton(_mockCallbackPublisher.Object);
  26. services.AddSingleton(_mockContentSerializer.Object);
  27. services.AddSingleton(_mockMessagePack.Object);
  28. _provider = services.BuildServiceProvider();
  29. }
  30. [Fact]
  31. public async void SendAsync_CanSend()
  32. {
  33. // Arrange
  34. _mockCallbackPublisher
  35. .Setup(x => x.PublishCallbackAsync(It.IsAny<CapPublishedMessage>()))
  36. .Returns(Task.CompletedTask).Verifiable();
  37. _mockContentSerializer
  38. .Setup(x => x.Serialize(It.IsAny<object>()))
  39. .Returns("").Verifiable();
  40. _mockMessagePack
  41. .Setup(x => x.Pack(It.IsAny<CapMessage>()))
  42. .Returns("").Verifiable();
  43. var fixture = Create();
  44. // Act
  45. await fixture.SendAsync(null, null, Mock.Of<object>());
  46. // Assert
  47. _mockCallbackPublisher.VerifyAll();
  48. _mockContentSerializer.Verify();
  49. _mockMessagePack.Verify();
  50. }
  51. private CallbackMessageSender Create()
  52. => _provider.GetService<CallbackMessageSender>();
  53. }
  54. }
  55. namespace Samples
  56. {
  57. public interface IFoo
  58. {
  59. int Age { get; set; }
  60. string Name { get; set; }
  61. }
  62. public class FooTest
  63. {
  64. [Fact]
  65. public void CanSetProperty()
  66. {
  67. var mockFoo = new Mock<IFoo>();
  68. mockFoo.Setup(x => x.Name).Returns("NameProerties");
  69. Assert.Equal("NameProerties", mockFoo.Object.Name);
  70. }
  71. }
  72. }