2
0

SshCommandTest_BeginExecute_EndExecuteNotInvokedOnAsyncResultFromPreviousInvocation.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Globalization;
  3. using System.Text;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using Moq;
  6. using Renci.SshNet.Channels;
  7. using Renci.SshNet.Tests.Common;
  8. namespace Renci.SshNet.Tests.Classes
  9. {
  10. [TestClass]
  11. public class SshCommandTest_BeginExecute_EndExecuteNotInvokedOnAsyncResultFromPreviousInvocation : TestBase
  12. {
  13. private Mock<ISession> _sessionMock;
  14. private Mock<IChannelSession> _channelSessionMock;
  15. private string _commandText;
  16. private Encoding _encoding;
  17. private SshCommand _sshCommand;
  18. private InvalidOperationException _actualException;
  19. protected override void OnInit()
  20. {
  21. base.OnInit();
  22. Arrange();
  23. Act();
  24. }
  25. private void Arrange()
  26. {
  27. var random = new Random();
  28. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  29. _channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
  30. _commandText = random.Next().ToString(CultureInfo.InvariantCulture);
  31. _encoding = Encoding.UTF8;
  32. var seq = new MockSequence();
  33. _sessionMock.InSequence(seq).Setup(p => p.CreateChannelSession()).Returns(_channelSessionMock.Object);
  34. _channelSessionMock.InSequence(seq).Setup(p => p.Open());
  35. _channelSessionMock.InSequence(seq).Setup(p => p.SendExecRequest(_commandText)).Returns(true);
  36. _sshCommand = new SshCommand(_sessionMock.Object, _commandText, _encoding);
  37. _sshCommand.BeginExecute();
  38. }
  39. private void Act()
  40. {
  41. try
  42. {
  43. _sshCommand.BeginExecute();
  44. Assert.Fail();
  45. }
  46. catch (InvalidOperationException ex)
  47. {
  48. _actualException = ex;
  49. }
  50. }
  51. [TestMethod]
  52. public void BeginExecuteShouldThrowInvalidOperationException()
  53. {
  54. Assert.IsNotNull(_actualException);
  55. Assert.IsNull(_actualException.InnerException);
  56. Assert.AreEqual("Asynchronous operation is already in progress.", _actualException.Message);
  57. }
  58. }
  59. }