PageRenderTime 26ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/mocks/MockMethod.cs

#
C# | 119 lines | 64 code | 25 blank | 30 comment | 9 complexity | 711338a49ee122d4bdd04050b0820c8a MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // Copyright 2007, Charlie Poole
  3. // This is free software licensed under the NUnit license. You may
  4. // obtain a copy of the license at http://nunit.org
  5. // ****************************************************************
  6. using System;
  7. using System.Collections;
  8. using NUnit.Framework;
  9. namespace NUnit.Mocks
  10. {
  11. /// <summary>
  12. /// The MockMethod object represents one named method on a mock object.
  13. /// All overloads are represented by one MockMethod. A method may return
  14. /// a fixed value, throw a fixed exception or have an expected sequence
  15. /// of calls. If it has a call sequence, then the signature must match and
  16. /// each call provides it's own return value or exception.
  17. /// </summary>
  18. public class MockMethod : IMethod
  19. {
  20. #region Private Fields
  21. /// <summary>
  22. /// Name of this method
  23. /// </summary>
  24. private string methodName;
  25. /// <summary>
  26. /// Fixed return value
  27. /// </summary>
  28. private object returnVal;
  29. /// <summary>
  30. /// Exception to be thrown
  31. /// </summary>
  32. private Exception exception;
  33. /// <summary>
  34. /// Expected call sequence. If null, this method has no expectations
  35. /// and simply provides a fixed return value or exception.
  36. /// </summary>
  37. private ArrayList expectedCalls = null;
  38. /// <summary>
  39. /// Actual sequence of calls... currently not used
  40. /// </summary>
  41. //private ArrayList actualCalls = null;
  42. #endregion
  43. #region Constructors
  44. public MockMethod( string methodName )
  45. : this( methodName, null, null ) { }
  46. public MockMethod( string methodName, object returnVal )
  47. : this( methodName, returnVal, null ) { }
  48. public MockMethod( string methodName, object returnVal, Exception exception )
  49. {
  50. this.methodName = methodName;
  51. this.returnVal = returnVal;
  52. this.exception = exception;
  53. }
  54. #endregion
  55. #region IMethod Members
  56. public string Name
  57. {
  58. get { return methodName; }
  59. }
  60. public void Expect( ICall call )
  61. {
  62. if ( expectedCalls == null )
  63. expectedCalls = new ArrayList();
  64. expectedCalls.Add( call );
  65. }
  66. #endregion
  67. #region ICall Members
  68. public object Call( object[] args )
  69. {
  70. if ( expectedCalls == null )
  71. {
  72. if ( exception != null )
  73. throw exception;
  74. return returnVal;
  75. }
  76. else
  77. {
  78. //actualCalls.Add( new MethodCall( methodName, null, null, args ) );
  79. Assert.IsTrue( expectedCalls.Count > 0, "Too many calls to " + Name );
  80. MockCall mockCall = (MockCall)expectedCalls[0];
  81. expectedCalls.RemoveAt( 0 );
  82. return mockCall.Call( args );
  83. }
  84. }
  85. #endregion
  86. #region IVerify Members
  87. public void Verify()
  88. {
  89. if ( expectedCalls != null )
  90. Assert.IsTrue( expectedCalls.Count == 0, "Not all methods were called" );
  91. }
  92. #endregion
  93. }
  94. }