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.

StateChangerTest.cs 1.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using DotNetCore.CAP.Infrastructure;
  3. using DotNetCore.CAP.Models;
  4. using DotNetCore.CAP.Processor.States;
  5. using Moq;
  6. using Xunit;
  7. namespace DotNetCore.CAP.Test
  8. {
  9. public class StateChangerTest
  10. {
  11. [Fact]
  12. public void ChangeState()
  13. {
  14. // Arrange
  15. var fixture = Create();
  16. var message = new CapPublishedMessage
  17. {
  18. StatusName = StatusName.Scheduled
  19. };
  20. var state = Mock.Of<IState>(s => s.Name == "s" && s.ExpiresAfter == null);
  21. var mockTransaction = new Mock<IStorageTransaction>();
  22. // Act
  23. fixture.ChangeState(message, state, mockTransaction.Object);
  24. // Assert
  25. Assert.Equal("s", message.StatusName);
  26. Assert.Null(message.ExpiresAt);
  27. Mock.Get(state).Verify(s => s.Apply(message, mockTransaction.Object), Times.Once);
  28. mockTransaction.Verify(t => t.UpdateMessage(message), Times.Once);
  29. mockTransaction.Verify(t => t.CommitAsync(), Times.Never);
  30. }
  31. [Fact]
  32. public void ChangeState_ExpiresAfter()
  33. {
  34. // Arrange
  35. var fixture = Create();
  36. var message = new CapPublishedMessage
  37. {
  38. StatusName = StatusName.Scheduled
  39. };
  40. var state = Mock.Of<IState>(s => s.Name == "s" && s.ExpiresAfter == TimeSpan.FromHours(1));
  41. var mockTransaction = new Mock<IStorageTransaction>();
  42. // Act
  43. fixture.ChangeState(message, state, mockTransaction.Object);
  44. // Assert
  45. Assert.Equal("s", message.StatusName);
  46. Assert.NotNull(message.ExpiresAt);
  47. mockTransaction.Verify(t => t.UpdateMessage(message), Times.Once);
  48. mockTransaction.Verify(t => t.CommitAsync(), Times.Never);
  49. }
  50. private StateChanger Create() => new StateChanger();
  51. }
  52. }