/// ------------------------------------------------------------------------------------ /// <summary> /// Override the Call method so that we can find the return value for the passed-in /// parameters. /// </summary> /// <param name="parameters"></param> /// <returns>The return value set up for the passed-in parameters; otherwise /// <c>null</c></returns> /// ------------------------------------------------------------------------------------ public override object Call(params object[] parameters) { // loop over all the set up method calls for (int i = 0; i < expectations.Count; i++) { MockCall mockCall = expectations[i]; if (parameters.Length != mockCall.ExpectedArgs.Length) { continue; } // Now compare all the parameters bool fFoundAllParams = true; for (int j = 0; j < parameters.Length; j++) { IConstraint constraint = mockCall.ExpectedArgs[j]; fFoundAllParams = (fFoundAllParams && constraint.Eval(parameters[j])); } if (fFoundAllParams) { return(mockCall.Call(signature.methodName, parameters)); } } return(null); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Adds a fixed return value for the passed in args. /// </summary> /// <param name="methodName">Name of the method/property</param> /// <param name="returnVal">Return value</param> /// <param name="args">Input parameters</param> /// ------------------------------------------------------------------------------------ public virtual void SetupResultForParams(string methodName, object returnVal, params object[] args) { AddMethodWithoutExpecations( new MethodSignature(Name, methodName, MockCall.GetArgTypes(args)), typeof(CallMethodWithParams), returnVal, null, args); }
public virtual object Call(params object[] parameters) { MockCall mockCall = expectations[timesCalled]; timesCalled++; return(mockCall.Call(signature.methodName, parameters)); }
public virtual void ExpectAndReturn(int nCount, string methodName, object result, params object[] args) { for (int i = 0; i < nCount; i++) { addExpectation(methodName, new MockCall(new MethodSignature(Name, methodName, MockCall.GetArgTypes(args)), result, null, args)); } }
/// ------------------------------------------------------------------------------------ /// <summary> /// Override the Call method so that we can wrap around if called to often /// </summary> /// <param name="parameters"></param> /// <returns></returns> /// ------------------------------------------------------------------------------------ public override object Call(params object[] parameters) { MockCall mockCall = expectations[timesCalled]; timesCalled++; if (timesCalled >= expectations.Count) { timesCalled = 0; } return(mockCall.Call(signature.methodName, parameters)); }
[Test] public void ArgTypes() { MockCall call = new MockCall( new MethodSignature("mymock", "call", new Type[] { typeof(int), typeof(string), typeof(bool[]) }), null, null, new object[] { 1, "string", new bool[] { true, false } }); Assertion.AssertEquals("Parameter one", typeof(int).FullName, call.ArgTypes[0]); Assertion.AssertEquals("Parameter two", typeof(string).FullName, call.ArgTypes[1]); Assertion.AssertEquals("Parameter three", typeof(bool[]).FullName, call.ArgTypes[2]); }
protected void addExpectation(string methodName, MockCall call) { MethodSignature signature = new MethodSignature(Name, methodName, call.ArgTypes); IMethod method = getMethod(signature); if (method == null) { method = new Method(signature); methods[methodName] = method; } method.SetExpectation(call); }
public virtual void SetExpectation(MockCall aCall) { expectations.Add(aCall); }
public void Add(MockCall aCall) { sequence.Add(aCall); }
public virtual void SetExpectation(MockCall call) { this.call = call; }
public virtual void ExpectAndThrow(string methodName, Exception e, params object[] args) { addExpectation(methodName, new MockCall(new MethodSignature(Name, methodName, MockCall.GetArgTypes(args)), null, e, args)); }
public virtual void SetExpectation(MockCall expectation) { this.expectation = expectation; }
[Test] public void ArgTypes() { MockCall call = new MockCall( new MethodSignature("mymock", "call", new Type[] {typeof(int), typeof(string), typeof(bool[]) }), null, null, new object[] {1, "string", new bool[] {true, false}}); Assertion.AssertEquals("Parameter one", typeof(int).FullName, call.ArgTypes[0]); Assertion.AssertEquals("Parameter two", typeof(string).FullName, call.ArgTypes[1]); Assertion.AssertEquals("Parameter three", typeof(bool[]).FullName, call.ArgTypes[2]); }