SshCommandTest_EndExecute_AsyncResultFromOtherInstance.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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_EndExecute_AsyncResultFromOtherInstance : TestBase
  12. {
  13. private Mock<ISession> _sessionMock;
  14. private Mock<IChannelSession> _channelSessionAMock;
  15. private Mock<IChannelSession> _channelSessionBMock;
  16. private string _commandText;
  17. private Encoding _encoding;
  18. private SshCommand _sshCommandA;
  19. private SshCommand _sshCommandB;
  20. private ArgumentException _actualException;
  21. private IAsyncResult _asyncResultB;
  22. protected override void OnInit()
  23. {
  24. base.OnInit();
  25. Arrange();
  26. Act();
  27. }
  28. private void Arrange()
  29. {
  30. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  31. _channelSessionAMock = new Mock<IChannelSession>(MockBehavior.Strict);
  32. _channelSessionBMock = new Mock<IChannelSession>(MockBehavior.Strict);
  33. _commandText = new Random().Next().ToString(CultureInfo.InvariantCulture);
  34. _encoding = Encoding.UTF8;
  35. _asyncResultB = null;
  36. var seq = new MockSequence();
  37. _sessionMock.InSequence(seq).Setup(p => p.CreateChannelSession()).Returns(_channelSessionAMock.Object);
  38. _channelSessionAMock.InSequence(seq).Setup(p => p.Open());
  39. _channelSessionAMock.InSequence(seq).Setup(p => p.SendExecRequest(_commandText)).Returns(true);
  40. _sessionMock.InSequence(seq).Setup(p => p.CreateChannelSession()).Returns(_channelSessionBMock.Object);
  41. _channelSessionBMock.InSequence(seq).Setup(p => p.Open());
  42. _channelSessionBMock.InSequence(seq).Setup(p => p.SendExecRequest(_commandText)).Returns(true);
  43. _sshCommandA = new SshCommand(_sessionMock.Object, _commandText, _encoding);
  44. _sshCommandA.BeginExecute();
  45. _sshCommandB = new SshCommand(_sessionMock.Object, _commandText, _encoding);
  46. _asyncResultB = _sshCommandB.BeginExecute();
  47. }
  48. private void Act()
  49. {
  50. try
  51. {
  52. _sshCommandA.EndExecute(_asyncResultB);
  53. Assert.Fail();
  54. }
  55. catch (ArgumentException ex)
  56. {
  57. _actualException = ex;
  58. }
  59. }
  60. [TestMethod]
  61. public void EndExecuteShouldHaveThrownArgumentException()
  62. {
  63. Assert.IsNotNull(_actualException);
  64. Assert.IsNull(_actualException.InnerException);
  65. Assert.AreEqual(string.Format("The {0} object was not returned from the corresponding asynchronous method on this class.", typeof(IAsyncResult).Name), _actualException.Message);
  66. Assert.IsNull(_actualException.ParamName);
  67. }
  68. }
  69. }