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/?p=license&r=2.4
5 // ****************************************************************
8 using System
.Collections
;
14 /// The MockMethod object represents one named method on a mock object.
15 /// All overloads are represented by one MockMethod. A method may return
16 /// a fixed value, throw a fixed exception or have an expected sequence
17 /// of calls. If it has a call sequence, then the signature must match and
18 /// each call provides it's own return value or exception.
20 public class MockMethod
: IMethod
22 #region Private Fields
25 /// Name of this method
27 private string methodName
;
30 /// Fixed return value
32 private object returnVal
;
35 /// Exception to be thrown
37 private Exception exception
;
40 /// Expected call sequence. If null, this method has no expectations
41 /// and simply provides a fixed return value or exception.
43 private ArrayList expectedCalls
= null;
46 /// Actual sequence of calls... currently not used
48 //private ArrayList actualCalls = null;
54 public MockMethod( string methodName
)
55 : this( methodName
, null, null ) { }
57 public MockMethod( string methodName
, object returnVal
)
58 : this( methodName
, returnVal
, null ) { }
60 public MockMethod( string methodName
, object returnVal
, Exception exception
)
62 this.methodName
= methodName
;
63 this.returnVal
= returnVal
;
64 this.exception
= exception
;
69 #region IMethod Members
73 get { return methodName; }
76 public void Expect( ICall call
)
78 if ( expectedCalls
== null )
79 expectedCalls
= new ArrayList();
81 expectedCalls
.Add( call
);
88 public object Call( object[] args
)
90 if ( expectedCalls
== null )
92 if ( exception
!= null )
99 //actualCalls.Add( new MethodCall( methodName, null, null, args ) );
100 Assert
.IsTrue( expectedCalls
.Count
> 0, "Too many calls to " + Name
);
101 MockCall mockCall
= (MockCall
)expectedCalls
[0];
102 expectedCalls
.RemoveAt( 0 );
103 return mockCall
.Call( args
);
109 #region IVerify Members
113 if ( expectedCalls
!= null )
114 Assert
.IsTrue( expectedCalls
.Count
== 0, "Not all methods were called" );