Guest User

Untitled

a guest
Jan 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. using FluentAssertions;
  2. using Moq;
  3. using System;
  4. using System.Threading.Tasks;
  5. using Xunit;
  6.  
  7. namespace MyUnitTestProject
  8. {
  9. public interface IMyInterface
  10. {
  11. int CoolNumber();
  12. Task<int> CoolNumberAsync();
  13. }
  14.  
  15. public class MyUnitTests
  16. {
  17. [Fact(DisplayName = "SetupSequence should return different results for subsequent calls")]
  18. public void SetupSequence_DifferentResults()
  19. {
  20. int expectedFirst = 5;
  21. int expectedSecond = 10;
  22. var myMock = new Mock<IMyInterface>();
  23.  
  24. // Each subsequent call to the method will return the next link in the chain
  25. myMock.SetupSequence(m => m.CoolNumber())
  26. .Returns(expectedFirst)
  27. .Returns(expectedSecond);
  28.  
  29. var first = myMock.Object.CoolNumber();
  30. var second = myMock.Object.CoolNumber();
  31.  
  32. first.Should().Be(expectedFirst);
  33. second.Should().Be(expectedSecond);
  34. }
  35.  
  36. [Fact(DisplayName = "SetupSequence should return different results for subsequent calls, even throwing exceptions!")]
  37. public void SetupSequence_FirstException()
  38. {
  39. bool firstExceptionThrown = false;
  40. int expectedSecond = 10;
  41. var myMock = new Mock<IMyInterface>();
  42.  
  43. // Works with throwing exceptions, too
  44. myMock.SetupSequence(m => m.CoolNumber())
  45. .Throws(new Exception("Something bad happened!"))
  46. .Returns(expectedSecond);
  47.  
  48. try
  49. {
  50. var first = myMock.Object.CoolNumber();
  51. }
  52. catch (Exception)
  53. {
  54. firstExceptionThrown = true;
  55. }
  56. var second = myMock.Object.CoolNumber();
  57.  
  58. firstExceptionThrown.Should().BeTrue();
  59. second.Should().Be(expectedSecond);
  60. }
  61.  
  62. [Fact(DisplayName = "SetupSequence should return different results for subsequent async calls")]
  63. public async Task SetupSequence_Async_DifferentResults()
  64. {
  65. int expectedFirst = 5;
  66. int expectedSecond = 10;
  67. var myMock = new Mock<IMyInterface>();
  68.  
  69. // If you have an async method, that's no problem
  70. myMock.SetupSequence(m => m.CoolNumberAsync())
  71. .ReturnsAsync(expectedFirst)
  72. .ReturnsAsync(expectedSecond);
  73.  
  74. var first = await myMock.Object.CoolNumberAsync();
  75. var second = await myMock.Object.CoolNumberAsync();
  76.  
  77. first.Should().Be(expectedFirst);
  78. second.Should().Be(expectedSecond);
  79. }
  80.  
  81. [Fact(DisplayName = "SetupSequence should return different results for subsequent async calls, even throwing exceptions!")]
  82. public async Task SetupSequence_Async_FirstException()
  83. {
  84. bool firstExceptionThrown = false;
  85. int expectedSecond = 10;
  86. var myMock = new Mock<IMyInterface>();
  87.  
  88. // Async throwing of exceptions, too.
  89. myMock.SetupSequence(m => m.CoolNumberAsync())
  90. .ThrowsAsync(new Exception("Something bad happened!"))
  91. .ReturnsAsync(expectedSecond);
  92.  
  93. try
  94. {
  95. var first = await myMock.Object.CoolNumberAsync();
  96. }
  97. catch (Exception)
  98. {
  99. firstExceptionThrown = true;
  100. }
  101. var second = await myMock.Object.CoolNumberAsync();
  102.  
  103. firstExceptionThrown.Should().BeTrue();
  104. second.Should().Be(expectedSecond);
  105. }
  106. }
  107. }
Add Comment
Please, Sign In to add comment