public void ForwardsToStringToIdentityProvider() { var toStringInvocation = new Invocation(receiver, TOSRING_METHOD, new object[0]); id.Invoke(toStringInvocation); Assert.AreEqual(identityProvider.ToString(), toStringInvocation.Result, "ToString()"); }
public void Invoke(Invocation invocation) { Assert.IsFalse(expectNotCalled, "MockInvokable should not have been invoked"); Actual = invocation; if (Expected != null) Assert.AreEqual(Expected.Method, Actual.Method, "method"); if (Outputs != null) { for (int i = 0; i < Actual.Parameters.Count; i++) { if (!Actual.Method.GetParameters()[i].IsIn) { Actual.Parameters[i] = Outputs[i]; } } } if (ThrownException != null) throw ThrownException; if (ExceptionSetOnInvocation != null) { invocation.Exception = ExceptionSetOnInvocation; } else if (invocation.Method.ReturnType != typeof (void)) { invocation.Result = ResultSetOnInvocation; } }
/// <summary> /// Invokes this object. /// </summary> /// <param name="invocation">The invocation.</param> public void Invoke(Invocation invocation) { Type returnType = invocation.Method.ReturnType; if (returnType == typeof (void)) { return; // sanity check } if (results.ContainsKey(returnType)) { IAction action = GetAction(returnType, results); action.Invoke(invocation); } else if (returnType.IsArray) { invocation.Result = NewEmptyArray(returnType); } else if (returnType.IsValueType) { invocation.Result = Activator.CreateInstance(returnType); } else if (defaultResults.ContainsKey(returnType)) { IAction action = GetAction(returnType, defaultResults); action.Invoke(invocation); } else { throw new InvalidOperationException("No action registered for return type " + returnType); } }
public void ForwardsGetHashCodeToIdentityProvider() { var getHashCodeInvocation = new Invocation(receiver, GETHASHCODE_METHOD, new object[0]); id.Invoke(getHashCodeInvocation); Assert.AreEqual(identityProvider.GetHashCode(), getHashCodeInvocation.Result, "GetHashCode()"); }
public void ForwardsInvocationsOfOtherMethodsToNextInChain() { var invocation = new Invocation(receiver, typeof (ICloneable).GetMethod("Clone"), new object[0]); id.Invoke(invocation); Assert.AreSame(invocation, next.Actual, "should have forwarded invocation to next in chain"); }
public static object ResultOfAction(IAction action) { object receiver = new NamedObject("receiver"); var methodInfo = new MethodInfoStub("method"); var invocation = new Invocation(receiver, methodInfo, new object[0]); action.Invoke(invocation); return invocation.Result; }
public void ForwardsInvocationsToNextIfMethodIsNotDeclaredInSpecifiedType() { var invocation = new Invocation(receiver, OTHER_METHOD, new object[0]); next.Expected = invocation; invoker.Invoke(invocation); Assert.IsFalse(target.FooWasInvoked, "should not have invoked method on target"); Assert.AreSame(invocation, next.Actual, "should have passed invocation to next in chain"); }
/// <summary> /// Executes the <paramref name="invocation"/> on the target of this instance /// if the targetType of this instance matches the invocation, otherwise the invocation /// is passed to the next <see cref="IInvokable"/> specified in the constructor. /// </summary> /// <param name="invocation">The invocation.</param> public void Invoke(Invocation invocation) { if (targetType == invocation.Method.DeclaringType) { invocation.InvokeOn(target); } else { next.Invoke(invocation); } }
public void SetsExceptionOfInvocation() { var exception = new Exception(); var action = new ThrowAction(exception); var receiver = new object(); var methodInfo = new MethodInfoStub("method"); var invocation = new Invocation(receiver, methodInfo, new object[0]); action.Invoke(invocation); Assert.AreSame(exception, invocation.Exception, "exception"); }
public void DoesNotMatchAndDoesNotThrowExceptionIfValueSpecifiedForOutputParameter() { Matcher matcher = new ArgumentsMatcher(Is.Same(arg1Value), Is.Same(arg2Value)); var invocation = new Invocation( new NamedObject("receiver"), new MethodInfoStub("method", new ParameterInfoStub("in", ParameterAttributes.In), new ParameterInfoStub("out", ParameterAttributes.Out)), new[] {arg1Value, null}); Assert.IsFalse(matcher.Matches(invocation)); }
public void SetsNamedParameterOnInvocationWrong() { var receiver = new object(); var methodInfo = new MethodInfoStub("method", new ParameterInfoStub("p1", ParameterAttributes.In), new ParameterInfoStub("p2", ParameterAttributes.Out)); string name = "p2_wrong"; var value = new object(); var invocation = new Invocation(receiver, methodInfo, new object[] {null, null}); var action = new SetNamedParameterAction(name, value); action.Invoke(invocation); Assert.AreSame(value, invocation.Parameters[1], "set value"); }
public void SetsIndexedParameterOnInvocation() { var receiver = new object(); var methodInfo = new MethodInfoStub("method", new ParameterInfoStub("p1", ParameterAttributes.In), new ParameterInfoStub("p2", ParameterAttributes.Out)); int index = 1; var value = new object(); var invocation = new Invocation(receiver, methodInfo, new object[] {null, null}); var action = new SetIndexedParameterAction(index, value); action.Invoke(invocation); Assert.AreSame(value, invocation.Parameters[index], "set value"); }
public void Invoke(Invocation invocation) { if (invocation.Method.DeclaringType == typeof (object)) { if (invocation.Method.Equals(EqualsMethod)) { invocation.Result = ReferenceEquals(invocation.Receiver, invocation.Parameters[0]); } else { invocation.InvokeOn(identityProvider); } } else { next.Invoke(invocation); } }
public override IMessage Invoke(IMessage msg) { var call = new MethodCall(msg); ParameterInfo[] parameters = call.MethodBase.GetParameters(); var invocation = new Invocation( GetTransparentProxy(), (MethodInfo) call.MethodBase, call.Args); invokable.Invoke(invocation); if (invocation.IsThrowing) { // TODO: it is impossible to set output parameters and throw an exception, // even though this is allowed by .NET method call semantics. return new ReturnMessage(invocation.Exception, call); } else { object[] outArgs = CollectOutputArguments(invocation, call, parameters); var methodInfo = (MethodInfo) call.MethodBase; if (invocation.Result == Missing.Value && methodInfo.ReturnType != typeof (void)) { throw new InvalidOperationException( string.Format( "You have to set the return value for method '{0}' on '{1}' mock.", call.MethodName, call.MethodBase.DeclaringType.Name)); } return new ReturnMessage( invocation.Result, outArgs, outArgs.Length, call.LogicalCallContext, call); } }
/// <summary> /// Determines whether all argument types of the generic method matches the invocation. /// </summary> /// <param name="invocation">The invocation to match against the initial argument types.</param> /// <returns> /// Returns true if invocation types matches the inital argument types; false otherwise. /// </returns> private bool MatchesTypeValues(Invocation invocation) { Type[] types = invocation.Method.GetGenericArguments(); for (int i = 0; i < types.Length; i++) { if (!typeMatchers[i].Matches(types[i])) { return false; } } return true; }
/// <summary> /// Determines whether the arguments of the invocation matches the initial arguments. /// </summary> /// <param name="invocation">The invocation to match against the initial arguments.</param> /// <returns> /// Returns true if invocation matches the initial arguments; false otherwise. /// </returns> private bool MatchesTypes(Invocation invocation) { return invocation.Method.GetGenericArguments().Length == typeMatchers.Length && MatchesTypeValues(invocation); }
public void Invoke(Invocation invocation) { invocationListener.NotifyInvocation(invocation); }
public void InvokesMethodOnObjectIfMethodIsDeclaredInSpecifiedType() { var invocation = new Invocation(receiver, FOO_METHOD, new object[0]); invoker.Invoke(invocation); Assert.IsTrue(target.FooWasInvoked, "Foo should have been invoked on target"); }
private object ProcessCallAgainstExpectations(Invocation invocationForMock) { Invoke(invocationForMock); if (invocationForMock.IsThrowing) { throw invocationForMock.Exception; } if (invocationForMock.Result == Missing.Value && invocationForMock.Method.ReturnType != typeof (void)) { throw new InvalidOperationException( string.Format( "You have to set the return value for method '{0}' on '{1}' mock.", invocationForMock.Method.Name, invocationForMock.Method.DeclaringType.Name)); } return invocationForMock.Result; }
public void ImplementsEqualsByComparingInvocationReceiversForIdentity() { next.ExpectNotCalled(); var equalInvocation = new Invocation(receiver, EQUALS_METHOD, new[] {receiver}); id.Invoke(equalInvocation); Assert.IsTrue((bool) equalInvocation.Result, "should have returned true"); object other = new NamedObject("other"); var notEqualInvocation = new Invocation(receiver, EQUALS_METHOD, new[] {other}); id.Invoke(notEqualInvocation); Assert.IsFalse((bool) notEqualInvocation.Result, "should not have returned true"); }
public void Dispatch(Invocation invocation) { if (Matches(invocation)) { Perform(invocation); } else { FailUnexpectedInvocation(invocation); } }
private void FailUnexpectedInvocation(Invocation invocation) { var description = new StringDescriptionWriter(); description.AppendText("unexpected invocation of "); invocation.DescribeOn(description); description.AppendNewLine(); DescribeActiveExpectationsTo(description); description.AppendList("\nstates:\n" , Environment.NewLine, string.Empty, stateMachines); // try catch to get exception with stack trace. throw new ExpectationException(description.ToString()); }
bool Matches(Invocation invocation) { return expectations.Any(e => e.Matches(invocation)); }
private bool ExtraMatchersMatch(Invocation invocation) { return extraMatchers.Cast<Matcher>().All(matcher => matcher.Matches(invocation)); }
public void Perform(Invocation invocation) { callCount++; foreach (IAction action in actions) { action.Invoke(invocation); } sideEffects.ForEach(sideEffect => sideEffect.Apply()); }
public bool Matches(Invocation invocation) { return IsActive && receiver == invocation.Receiver && methodMatcher.Matches(invocation.Method) && argumentsMatcher.Matches(invocation) && ExtraMatchersMatch(invocation) && GenericMethodTypeMatcher.Matches(invocation) && IsInCorrectOrder(); }
public void MatchesOutputParametersWithSpecialMatcherClass() { Matcher matcher = new ArgumentsMatcher(Is.Same(arg1Value), Is.Out); var invocation = new Invocation( new NamedObject("receiver"), new MethodInfoStub("method", new ParameterInfoStub("in", ParameterAttributes.In), new ParameterInfoStub("out", ParameterAttributes.Out)), new[] {arg1Value, null}); Assert.IsTrue(matcher.Matches(invocation)); }
public void Intercept(IInvocation interceptedInvocation) { // Check for calls to basic NMock2 infrastructure if (mockObjectMethods.ContainsKey(interceptedInvocation.Method)) { try { interceptedInvocation.ReturnValue = interceptedInvocation.Method.Invoke(this, interceptedInvocation. Arguments); } catch (TargetInvocationException tie) { // replace stack trace with original stack trace FieldInfo remoteStackTraceString = typeof (Exception).GetField("_remoteStackTraceString", BindingFlags.Instance | BindingFlags.NonPublic); remoteStackTraceString.SetValue(tie.InnerException, tie.InnerException.StackTrace + Environment.NewLine); throw tie.InnerException; } return; } // Ok, this call is targeting a member of the mocked class object invocationTarget = GetInvocationTarget(interceptedInvocation); var invocationForMock = new Invocation( invocationTarget, interceptedInvocation.Method, interceptedInvocation.Arguments); interceptedInvocation.ReturnValue = ProcessCallAgainstExpectations(invocationForMock); }
public bool MatchesIgnoringIsActive(Invocation invocation) { return receiver == invocation.Receiver && methodMatcher.Matches(invocation.Method) && argumentsMatcher.Matches(invocation) && ExtraMatchersMatch(invocation) && GenericMethodTypeMatcher.Matches(invocation); }
void Perform(Invocation invocation) { foreach (IExpectation e in expectations.Where(e => e.Matches(invocation))) { e.Perform(invocation); return; } throw new InvalidOperationException("No matching expectation"); }