/// <summary> /// Handles the method /// </summary> /// <param name="method"></param> /// <param name="arguments"></param> public override void HandleMethodCall(MethodInfo method, object[] arguments) { Method = method; if (ArgumentManager.HasBeenUsed) { ArgumentManager.ValidateMethodSignature(method); Arguments = ArgumentManager.GetConstraints(); ReturnArguments = ArgumentManager.GetReturnValues(); SetExpectedCount(new Range(1, null)); ArgumentManager.Clear(); return; } var parameters = method.GetParameters(); var constraints = new AbstractConstraint[parameters.Length]; for (int index = 0; index < parameters.Length; index++) { var parameter = parameters[index]; constraints[index] = (parameter.IsOut) ? Is.Anything() : Is.Equal(arguments[index]); } Arguments = constraints; }
/// <summary> /// Constructs an instance of the Mock Repository. /// </summary> /// <remarks> /// There is very little reason to instantiate an instance of MockRepository. /// This is intended for internal use only. /// </remarks> public MockRepository() { delegateRepository = new RepositoryForDelegates(); generatorOptions = new ProxyGenerationOptions { Selector = new RhinoSelector(), }; generatorOptions.AdditionalAttributes .Add(AttributeUtil.CreateBuilder <__ProtectAttribute>()); defaultOptions = new ProxyGenerationOptions { Selector = new RhinoSelector(), }; ArgumentManager.Clear(); }
/// <summary> /// Handles the call /// </summary> /// <param name="method"></param> /// <param name="arguments"></param> public override void HandleMethodCall(MethodInfo method, object[] arguments) { if (!method.IsSpecialName) { throw new InvalidOperationException("Property expectations can only be set for properties."); } var methodName = method.Name; if (!methodName.StartsWith("get_") && !methodName.StartsWith("set_")) { throw new InvalidOperationException("Property expectations can only be set for properties."); } var propertyName = method.Name.Substring(4); PropertyInfo property; if (methodName.StartsWith("get_")) { var types = new Type[arguments.Length]; if (arguments.Length > 0) { for (int argumentIndex = 0; argumentIndex < arguments.Length; argumentIndex++) { types[argumentIndex] = arguments[argumentIndex].GetType(); } } property = method.DeclaringType.GetProperty(propertyName, types); } else { var argumentLength = (arguments.Length - 1); var types = new Type[argumentLength]; if (argumentLength > 0) { for (int argumentIndex = 0; argumentIndex < argumentLength; argumentIndex++) { types[argumentIndex] = arguments[argumentIndex].GetType(); } } property = method.DeclaringType.GetProperty(propertyName, types); } MethodGet = property.GetGetMethod(true); MethodSet = property.GetSetMethod(true); if (MethodGet == null || MethodSet == null) { throw new InvalidOperationException("Property must be read/write."); } if (ArgumentManager.HasBeenUsed) { ArgumentManager.ValidateMethodSignature(method); Arguments = ArgumentManager.GetConstraints(); ReturnArguments = ArgumentManager.GetReturnValues(); SetExpectedCount(new Range(1, null)); ArgumentManager.Clear(); return; } var parameters = method.GetParameters(); var constraints = new AbstractConstraint[parameters.Length]; for (int index = 0; index < parameters.Length; index++) { var parameter = parameters[index]; constraints[index] = (parameter.IsOut) ? Is.Anything() : Is.Equal(arguments[index]); } Arguments = constraints; }