From: Joe W. <joe...@us...> - 2002-10-06 22:57:51
|
Update of /cvsroot/mockobjects/nmock/src/NMock In directory usw-pr-cvs1:/tmp/cvs-serv30372/src/NMock Modified Files: IMock.cs Mock.cs Log Message: Added Mock.SetValue() for setting up methods/properties that always return the same value and have no expectations about how they are called (i.e. getter methods). Index: IMock.cs =================================================================== RCS file: /cvsroot/mockobjects/nmock/src/NMock/IMock.cs,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -u -r1.1.1.1 -r1.2 --- IMock.cs 6 Oct 2002 20:49:08 -0000 1.1.1.1 +++ IMock.cs 6 Oct 2002 22:57:48 -0000 1.2 @@ -37,6 +37,13 @@ void ExpectAndThrow(string methodName, Exception exceptionVal, params object[] args); /// <summary> + /// Set a fixed return value for a method/property. This allows the method to be + /// called multiple times in no particular sequence and have the same value returned + /// each time. Useful for getter style methods. + /// </summary> + void SetValue(string methodName, object returnVal); + + /// <summary> /// Make a call to a mocked up method name to check it meets its expectations. /// </summary> object Call(string methodName, params object[] args); Index: Mock.cs =================================================================== RCS file: /cvsroot/mockobjects/nmock/src/NMock/Mock.cs,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -u -r1.1.1.1 -r1.2 --- Mock.cs 6 Oct 2002 20:49:08 -0000 1.1.1.1 +++ Mock.cs 6 Oct 2002 22:57:48 -0000 1.2 @@ -12,11 +12,13 @@ private string name; private object obj; private IList expectations; + private IDictionary values; public Mock(string name) { this.name = name; expectations = new ArrayList(); + values = new Hashtable(); currentExpectation = 0; } @@ -52,8 +54,17 @@ expectations.Add(new Expectation(methodName, null, e, args)); } + public virtual void SetValue(string methodName, object returnVal) + { + values[methodName] = returnVal; + } + public virtual object Call(string methodName, params object[] args) { + if (values.Contains(methodName)) + { + return values[methodName]; + } if (currentExpectation == expectations.Count) { Assertion.Fail(methodName + "() called too many times"); |