/// <summary> /// Gets the all expectations for a mocked object and method combination, /// regardless of the expected arguments / callbacks / contraints. /// </summary> /// <param name="proxy">Mocked object.</param> /// <param name="method">Method.</param> /// <returns>List of all relevant expectation</returns> public override ExpectationsList GetAllExpectationsForProxyAndMethod(object proxy, MethodInfo method) { Validate.IsNotNull(proxy, "proxy"); Validate.IsNotNull(method, "method"); ExpectationsList expectations = new ExpectationsList(); foreach (object action in recordedActions) { ProxyMethodExpectationTriplet triplet = action as ProxyMethodExpectationTriplet; if (triplet != null) { if (MockedObjectsEquality.Instance.Equals(triplet.Proxy, proxy) && MethodsEquals(method, triplet)) { expectations.Add(triplet.Expectation); } } else //Action is another recorder { IMethodRecorder innerRecorder = (IMethodRecorder)action; expectations.AddRange(innerRecorder.GetAllExpectationsForProxyAndMethod(proxy, method)); } } return(expectations); }
/// <summary> /// This check the methods that were setup using the SetupResult.For() /// or LastCall.Repeat.Any() and that bypass the whole expectation model. /// </summary> public IExpectation GetRepeatableExpectation(object proxy, MethodInfo method, object[] args) { ProxyMethodPair pair = new ProxyMethodPair(proxy, method); if (repeatableMethods.ContainsKey(pair) == false) { return(null); } ExpectationsList list = repeatableMethods[pair]; foreach (IExpectation expectation in list) { if (expectation.IsExpected(args)) { expectation.AddActualCall(); if (expectation.RepeatableOption == RepeatableOption.Never) { string errMsg = string.Format("{0} Expected #{1}, Actual #{2}.", expectation.ErrorMessage, expectation.Expected, expectation.ActualCallsCount); ExpectationViolationException exception = new ExpectationViolationException(errMsg); MockRepository.SetExceptionToBeThrownOnVerify(proxy, exception); throw exception; } return(expectation); } } return(null); }
/// <summary> /// Gets the all expectations for proxy. /// </summary> /// <param name="proxy">Mocked object.</param> /// <returns>List of all relevant expectation</returns> protected override ExpectationsList DoGetAllExpectationsForProxy(object proxy) { Validate.IsNotNull(proxy, "proxy"); ExpectationsList expectations = new ExpectationsList(); foreach (object action in recordedActions) { ProxyMethodExpectationTriplet triplet = action as ProxyMethodExpectationTriplet; if (triplet != null) { if (MockedObjectsEquality.Instance.Equals(triplet.Proxy, proxy)) { expectations.Add(triplet.Expectation); } } else //Action is another recorder { IMethodRecorder innerRecorder = (IMethodRecorder)action; ExpectationsList expectationsForProxy = innerRecorder.GetAllExpectationsForProxy(proxy); expectations.AddRange(expectationsForProxy); } } return(expectations); }
public void GetAllExpectationsForProxy() { recorder.Record(this.demo, this.voidNoArgs, expectationOne); recorder.Record(this.demo, this.voidThreeArgs, expectationOne); recorder.Record(this.demo, this.voidNoArgs, expectationOne); ExpectationsList expectations = recorder.GetAllExpectationsForProxy(demo); Assert.Equal(3, expectations.Count); }
public void ReplaceExpectation() { recorder.Record(this.demo, this.voidNoArgs, expectationOne); AnyArgsExpectation newExpectation = new AnyArgsExpectation(new FakeInvocation(voidNoArgs), new Range(1, 1)); recorder.ReplaceExpectation(demo, voidNoArgs, expectationOne, newExpectation); ExpectationsList list = recorder.GetAllExpectationsForProxyAndMethod(demo, voidNoArgs); Assert.Same(newExpectation, list[0]); }
public void GetAllExpectationsForProxyWithNestedOrdering() { recorder.AddRecorder(this.CreateRecorder()); recorder.Record(this.demo, this.voidNoArgs, expectationTwo); recorder.Record(this.demo, this.voidThreeArgs, expectationOne); //move to replayer recorder.GetRecordedExpectation(new FakeInvocation(voidNoArgs), demo, voidNoArgs, new object[0]); ExpectationsList expectations = recorder.GetAllExpectationsForProxy(demo); Assert.Equal(1, expectations.Count); }
public void GetAllExpectationForProxyAndMethod() { recorder.Record(this.demo, this.voidNoArgs, expectationOne); recorder.Record(this.demo, this.voidThreeArgs, expectationOne); recorder.Record(this.demo, this.voidNoArgs, expectationOne); ExpectationsList expectations = recorder.GetAllExpectationsForProxyAndMethod(demo, voidNoArgs); Assert.Equal(2, expectations.Count); expectations = recorder.GetAllExpectationsForProxyAndMethod(demo, voidThreeArgs); Assert.Equal(1, expectations.Count); }
private void Calculate(object proxy, MethodInfo method, object[] args) { ExpectationsList list = parent.GetAllExpectationsForProxyAndMethod(proxy, method); foreach (IExpectation expectation in list) { if (expectation.IsExpected(args)) { expectedMin += expectation.Expected.Min; expectedMax += expectation.Expected.Max ?? expectation.Expected.Min; actual += expectation.ActualCallsCount; } } }
private bool TryReplaceRepeatableExpectation(MethodInfo method, IExpectation newExpectation, IExpectation oldExpectation, object proxy) { ProxyMethodPair pair = new ProxyMethodPair(proxy, method); if (repeatableMethods.ContainsKey(pair)) { ExpectationsList expectationsList = repeatableMethods[pair]; int indexOf = expectationsList.IndexOf(oldExpectation); if (indexOf != -1) { expectationsList[indexOf] = newExpectation; return(true); } } return(false); }
private void AppendNextExpected(object proxy, MethodInfo method, StringBuilder sb) { ExpectationsList list = GetAllExpectationsForProxyAndMethod(proxy, method); if (list.Count > 0) { IExpectation expectation = list[0]; if (expectation.ExpectationSatisfied) { return; //avoid showing methods that were completed. } sb.Append("\r\n"); sb.Append(expectation.ErrorMessage).Append(" Expected #"); sb.Append(expectation.Expected).Append(", Actual #"); sb.Append(expectation.ActualCallsCount).Append("."); } }
private void ExpectationNotOnList(ExpectationsList list, IExpectation expectation, bool isStub) { bool expectationExists = list.Contains(expectation); if (expectationExists == false) { return; } bool isProeprty = expectation.Method.IsSpecialName && (expectation.Method.Name.StartsWith("get_") || expectation.Method.Name.StartsWith("set_")); if (isStub == false || isProeprty == false) { throw new InvalidOperationException("The result for " + expectation.ErrorMessage + " has already been setup."); } throw new InvalidOperationException("The result for " + expectation.ErrorMessage + " has already been setup. Properties are already stubbed with PropertyBehavior by default, no action is required"); }
/// <summary> /// Set the expectation so it can repeat any number of times. /// </summary> public void AddToRepeatableMethods(object proxy, MethodInfo method, IExpectation expectation) { //Just to get an error if I make a mistake and try to add a normal //method to the speical repeat method Debug.Assert(expectation.RepeatableOption != RepeatableOption.Normal); RemoveExpectation(expectation); ProxyMethodPair pair = new ProxyMethodPair(proxy, method); if (repeatableMethods.ContainsKey(pair) == false) { repeatableMethods.Add(pair, new ExpectationsList()); } ExpectationsList expectationsList = repeatableMethods[pair]; ExpectationNotOnList(expectationsList, expectation, MockRepository.IsStub(proxy)); expectationsList.Add(expectation); }
public void RemoveExpectationWhenNestedOrdering() { IExpectation newExpectation = new ArgsEqualExpectation(new FakeInvocation(voidThreeArgs), new object[] { 1, null, 1f }, new Range(1, 1)); recorder.Record(this.demo, this.voidNoArgs, expectationOne); recorder.AddRecorder(CreateRecorder()); recorder.Record(this.demo, this.voidThreeArgs, expectationTwo); recorder.Record(this.demo, this.voidNoArgs, newExpectation); recorder.RemoveExpectation(expectationTwo); //move to replayer, but also remove one expectation from consideration recorder.GetRecordedExpectation(new FakeInvocation(voidNoArgs), demo, voidNoArgs, new object[0]); ExpectationsList expectations = recorder.GetAllExpectationsForProxy(demo); Assert.Equal(1, expectations.Count); Assert.Equal(expectations[0], newExpectation); }
private static ExpectationVerificationInformation GetExpectationsToVerify <T>(T mock, Action <T> action, Action <IMethodOptions <object> > setupConstraints) { IMockedObject mockedObject = MockRepository.GetMockedObject(mock); MockRepository mocks = mockedObject.Repository; if (mocks.IsInReplayMode(mockedObject) == false) { throw new InvalidOperationException( "Cannot assert on an object that is not in replay mode. Did you forget to call ReplayAll() ?"); } var mockToRecordExpectation = (T)mocks.DynamicMock(FindAppropriteType <T>(mockedObject), mockedObject.ConstructorArguments); action(mockToRecordExpectation); AssertExactlySingleExpectaton(mocks, mockToRecordExpectation); IMethodOptions <object> lastMethodCall = mocks.LastMethodCall <object>(mockToRecordExpectation); lastMethodCall.TentativeReturn(); if (setupConstraints != null) { setupConstraints(lastMethodCall); } ExpectationsList expectationsToVerify = mocks.Replayer.GetAllExpectationsForProxy(mockToRecordExpectation); if (expectationsToVerify.Count == 0) { throw new InvalidOperationException( "The expectation was removed from the waiting expectations list, did you call Repeat.Any() ? This is not supported in AssertWasCalled()"); } IExpectation expected = expectationsToVerify[0]; ICollection <object[]> argumentsForAllCalls = mockedObject.GetCallArgumentsFor(expected.Method); return(new ExpectationVerificationInformation { ArgumentsForAllCalls = new List <object[]>(argumentsForAllCalls), Expected = expected }); }
/// <summary> /// Gets the all expectations for proxy. /// </summary> /// <param name="proxy">Mocked object.</param> /// <returns>List of all relevant expectation</returns> public ExpectationsList GetAllExpectationsForProxy(object proxy) { ExpectationsList fromChild = null, mine; if (replayerToCall != null) { fromChild = replayerToCall.GetAllExpectationsForProxy(proxy); } mine = DoGetAllExpectationsForProxy(proxy); if (fromChild != null) { foreach (IExpectation expectation in fromChild) { if (mine.Contains(expectation) == false) { mine.Add(expectation); } } } return(mine); }
/// <summary> /// Create an exception for an unexpected method call. /// </summary> public override ExpectationViolationException UnexpectedMethodCall(IInvocation invocation, object proxy, MethodInfo method, object[] args) { // We have move to the parent recorder, we need to pass the call to it. if (parentRecorderRedirection != null) { return(parentRecorderRedirection.UnexpectedMethodCall(invocation, proxy, method, args)); } StringBuilder sb = new StringBuilder(); CalcExpectedAndActual calc = new CalcExpectedAndActual(this, proxy, method, args); string methodAsString = MethodCallUtil.StringPresentation(invocation, method, args); sb.Append(methodAsString); sb.Append(" Expected #"); if (calc.ExpectedMax == calc.ExpectedMin) { sb.Append(calc.ExpectedMin); } else { sb.Append(calc.ExpectedMin).Append(" - ").Append(calc.ExpectedMax); } sb.Append(", Actual #").Append(calc.Actual).Append('.'); ExpectationsList list = GetAllExpectationsForProxyAndMethod(proxy, method); if (list.Count > 0) { string message = list[0].Message; if (message != null) { sb.Append(System.Environment.NewLine) .Append("Message: ") .Append(message); } } AppendNextExpected(proxy, method, sb); return(new ExpectationViolationException(sb.ToString())); }
private void ExpectationNotOnList(ExpectationsList list, IExpectation expectation, bool isStub) { bool expectationExists = list.Contains(expectation); if (expectationExists == false) return; bool isProeprty = expectation.Method.IsSpecialName && (expectation.Method.Name.StartsWith("get_") || expectation.Method.Name.StartsWith("set_")); if (isStub == false || isProeprty == false) throw new InvalidOperationException("The result for " + expectation.ErrorMessage + " has already been setup."); throw new InvalidOperationException("The result for " + expectation.ErrorMessage + " has already been setup. Properties are already stubbed with PropertyBehavior by default, no action is required"); }
/// <summary> /// Gets the all expectations for proxy. /// </summary> /// <param name="proxy">Mocked object.</param> /// <returns>List of all relevant expectation</returns> protected override ExpectationsList DoGetAllExpectationsForProxy(object proxy) { Validate.IsNotNull(proxy, "proxy"); ExpectationsList expectations = new ExpectationsList(); foreach (object action in recordedActions) { ProxyMethodExpectationTriplet triplet = action as ProxyMethodExpectationTriplet; if (triplet != null) { if (MockedObjectsEquality.Instance.Equals( triplet.Proxy , proxy)) { expectations.Add(triplet.Expectation); } } else //Action is another recorder { IMethodRecorder innerRecorder = (IMethodRecorder) action; ExpectationsList expectationsForProxy = innerRecorder.GetAllExpectationsForProxy(proxy); expectations.AddRange(expectationsForProxy); } } return expectations; }
/// <summary> /// Gets the all expectations for a mocked object and method combination, /// regardless of the expected arguments / callbacks / contraints. /// </summary> /// <param name="proxy">Mocked object.</param> /// <param name="method">Method.</param> /// <returns>List of all relevant expectation</returns> public override ExpectationsList GetAllExpectationsForProxyAndMethod(object proxy, MethodInfo method) { Validate.IsNotNull(proxy, "proxy"); Validate.IsNotNull(method, "method"); ExpectationsList expectations = new ExpectationsList(); foreach (object action in recordedActions) { ProxyMethodExpectationTriplet triplet = action as ProxyMethodExpectationTriplet; if (triplet != null) { if (MockedObjectsEquality.Instance.Equals( triplet.Proxy , proxy ) && MethodsEquals(method, triplet)) { expectations.Add(triplet.Expectation); } } else //Action is another recorder { IMethodRecorder innerRecorder = (IMethodRecorder) action; expectations.AddRange(innerRecorder.GetAllExpectationsForProxyAndMethod(proxy, method)); } } return expectations; }