2 using System.Collections;
3 using System.Runtime.Remoting.Proxies;
4 using System.Runtime.Remoting.Messaging;
10 /// Summary description for MockObject.
12 public class Mock : IMock
14 #region Private Fields
20 private IDictionary methods = new Hashtable();
22 private Exception lastException;
28 public Exception LastException
30 get { return lastException; }
37 public Mock() : this( "Mock" ) { }
39 public Mock( string name )
55 get { return strict; }
56 set { strict = value; }
59 public void Expect( string methodName, params object[] args )
61 ExpectAndReturn( methodName, null, args );
64 public void Expect( string methodName )
66 ExpectAndReturn( methodName, null, null );
69 public void ExpectNoCall( string methodName )
71 methods[methodName] = new MockMethod( methodName, null,
72 new AssertionException("Unexpected call to method " + methodName) );
75 public void ExpectAndReturn( string methodName, object returnVal, params object[] args )
77 AddExpectedCall( methodName, returnVal, null, args );
80 public void ExpectAndThrow( string methodName, Exception exception, params object[] args )
82 AddExpectedCall( methodName, null, exception, args );
85 public void SetReturnValue( string methodName, object returnVal )
87 methods[methodName] = new MockMethod( methodName, returnVal );
92 #region IVerify Members
94 public virtual void Verify()
96 foreach( IMethod method in methods.Values )
102 #region ICallHandler Members
104 public virtual object Call( string methodName, params object[] args )
106 if ( methods.Contains( methodName ) )
110 IMethod method = (IMethod)methods[methodName];
111 return method.Call( args );
113 catch( Exception exception )
115 // Save exception in case MO is running on a separate thread
116 lastException = exception;
120 else // methodName is not listed in methods
122 Assert.Fail( "Unexpected call to " + methodName );
124 // not listed but Strict is not specified
130 #region Helper Methods
132 private void AddExpectedCall( string methodName, object returnVal, Exception exception, object[] args )
134 IMethod method = (IMethod)methods[methodName];
135 if ( method == null )
137 method = new MockMethod( methodName );
138 methods[methodName] = method;
141 Type[] argTypes = MethodSignature.GetArgTypes( args );
142 MethodSignature signature = new MethodSignature( this.Name, methodName, argTypes );
144 method.Expect( new MockCall( signature, returnVal, exception, args ) );